Euler Problem 64

Euler Problem 64:How many continued fractions for N ≤ 10000 have an odd period?

In Mathematica:

```oddFractionCount = 0;
For[i = 2, i &lt;= 10000, i++,
If[IntegerPart[Sqrt[i]] == Sqrt[i], Continue[]];
If[Mod[Length[ContinuedFraction[Sqrt[i]][[2]]], 2] == 1,
oddFractionCount++]]; oddFractionCount
```

C# Implementacija:

Iskorištena Metoda iz prethodnog problema broj 66, koja izračunava sve kontinualne frakcije:

```using System;
using System.Collections.Generic;
namespace EulerProblem66
{
class Program
{
static void Main(string[] args)
{
int oddPeriod = 0;
for (int d = 2; d <= 10000; d++)
{
if (d % Math.Sqrt(d) == 0)
continue;
List<long> c = ContinedFraction(d);
if (c.Count % 2 == 0)
oddPeriod++;

}
Console.WriteLine(oddPeriod);
///Kraj
Console.WriteLine("Pres any key to continue..");
}

static List<long> ContinedFraction(long d)
{
List<long> e = new List<long>();
long d2 = (long)Math.Sqrt(d);
long a = d2;
long p = 0;
long q = 1;
do
{
p = a * q - p;
q = (d - p * p) / q;
a = (p + d2) / q;
} while (q != 1);

return e;
}

}
}
```

Euler Problem 66

Euler Problem 66:Investigate the Diophantine equation x2 − Dy2 = 1.

Varijanta 1. Korištenjem Mathematica

Rješenje se može vrlo lako dobiti korištenjem Wolframove Mathematike:

```maxX = 0;
Dmin = 0;
For[d = 2, d <= 1000, d++,
If[Mod[Length[Divisors[d]], 2] == 1, Continue[]];
solX = x /.FindInstance[x^2 - d*y^2 == 1 && x > 0 && y > 0, {x, y},Integers][[1]];
If[solX > maxX, maxX = solX; Dmin = d]]; Dmin
```

Varijanta 2: C# Implementacija sa BigNumber strukturom:

```using System;
using System.Collections.Generic;
using System.Numerics;
namespace EulerProblem66
{
class Program
{
static void Main(string[] args)
{
BigInteger x = 0;
BigInteger maxx = 0;
int maxd = 0;
for (int d = 2; d <= 1000; d++)
{
if (d % Math.Sqrt(d) == 0)
continue;
List<long> c = ContinedFraction(d);
x = PellEq(c);
if (x > maxx)
{
maxx = x;
maxd = d;
}
}
Console.WriteLine(maxd);
///Kraj
Console.WriteLine("Pres any key to continue..");
}
static long GCD(long a, long b)
{
long r = a % b;
if (r == 0)
return b;
else
return GCD(b, r);
}
static List<long> ContinedFraction(long d)
{
List<long> e = new List<long>();
long d2 = (long)Math.Sqrt(d);
long a = d2;
long p = 0;
long q = 1;
do
{
p = a * q - p;
q = (d - p * p) / q;
a = (p + d2) / q;
} while (q != 1);

return e;
}
static BigInteger PellEq(List<long> c)
{
int l = c.Count - 1;
int per = l % 2 == 0 ? l - 1 : 2 * l - 1;

BigInteger a = c[0];
BigInteger a1 = 1;
BigInteger b = 1;
BigInteger b1 = 0;
BigInteger t;

a = c[0];
b = c[0] * b + b1;
for (int i = 1; i <= per; i++)
{
t = a;
a = c[(i - 1) % l + 1] * a + a1;
a1 = t;
t = b;
b = c[(i - 1) % l + 1] * b + b1;
b1 = t;
}
return a;
}
}
}
```

Partitioners in ParallelFX

As you already know the parallel processing on multicore processors will be supported on the next version of .NET Framework. The .NET Framework 4.0 and Visual Studio 2010 will be release in spring of this year. In this post I am going to talk about Partitiones in ParallelFX.
Partition of data source play important role in parallel data processing. PLINQ and TLP have built-in partition concept which in certain situation and advanced scenarios, does not produce efficient results.
Two main kinds of partitioning existing in ParallelFX.

Range partitioning: When we talk about collections of data such as lists or arrays with known length, range partitioning is a commonly used and the simplest way of partitioning. Each thread receives a certain amount of data from the initial to the final index and there is no possibility that two or more threads access the same information in the list. The only overhead involved in range partitioning is the initial work of creating the ranges; no additional synchronization is required after that. This partitioning method is very efficient when we have constant workload by all data in the list. Drawback of this partitioning is the situation when one thread is finish early, and cannot help other to finish their work.

Chunk partitioning: When we have collections with an unknown number of elements, such as LinkedList etc., the possibility of partitioning comes down to it that every thread takes a number of elements from the list – “chunk size” and processes them. When it completes, it  returns and take another  chunk. This implementation of partitioning is done in a way that no one thread can take  the same element in collection.  Chunk size is arbitrary and can be from 1 to several elements, all depends on the nature of the operation. The Chunk partitioner does incur the synchronization overhead each time the thread needs to get another chunk. The amount of synchronization incurred in these cases is inversely proportional to the size of the chunks.
The TPL partitioners support dynamic number of partitions, they are created on the fly when the parallel loop spawns a new task.

Partitioner Class
Partitioners Class provides common partitioning strategies for arrays, lists, and enumerables. It contains only one methods, and several overloaded versions, which are presented in below text.

```public static class Partitioner
{
public static OrderablePartitioner Create(IEnumerable source);
public static OrderablePartitioner Create(IList list, bool loadBalance);
public static OrderablePartitioner> Create(int fromInclusive, int toExclusive);
public static OrderablePartitioner> Create(long fromInclusive, long toExclusive);
public static OrderablePartitioner Create(TSource[] array, bool loadBalance);
public static OrderablePartitioner> Create(int fromInclusive, int toExclusive, int rangeSize);
public static OrderablePartitioner> Create(long fromInclusive, long toExclusive, long rangeSize);
}
```

Demo

In this demo we will  show some of custom partitions implementations.Let’s assume that we have to count prime numbers below a 500 000. In this example we have a method for prime number check which is implemented below.

```
public bool IsPrime(long n)

{
for (long i = 2; i * i < n; i++)
{
if (n % i == 0)
return false;
else
return true;
}
}
```

The method return true if n is prime, otherwise returns false. The interval for prime number checking is  [1,500 000],  so the code implementation is:

```
var src = Enumerable.Range(1, 500000).ToArray();

```

I Implementation is pure sequential, and it contains simple LINQ query as the following:

```var count = (from p in src
where IsPrime(p)
select p).Count();
```

As we know this code will be executed on one core of multicore processors.

II Implementation is implementation of the standard PLINQ query similar as above:

```
var count = (from p in src.AsParallel()
where IsPrime(p)
select p).Count();
```

The differences of mentioned implementation is in AsParallel(), word in second implementation. III Implementation is implementation of custom partitioner with Partitioners class. We create partitioner with Create method, and ParallelFX does the rest.

```var count2 = Partitioner.Create(src, true).AsParallel().
Where(p => IsPrime(p)).
Select(p => p).Count();
```

The test is created on Quad Core Intel processors, and the result is presented on the following picture.

Summary

The new Partitioner class allows to define custom partitioner which can be in some situations more efficient than default partitioner in PLINQ. Use custom partitioner in PLINQ query when you try to process no constant operations for every element on data source.
Reference:
1. http://msdn.microsoft.com/en-us/library/dd460693(VS.100).aspx
2. http://blogs.msdn.com/pfxteam/