Skip to main content
1 of 2

i found an answer in Stack Overflow a few days ago, i adapted it a bit to make it more parallel and easier to read.

--> i might have made a mistake, mind me if i did. I tested it in my computer. Core i7 Second Gen (3Ghz). 12Gb of ram.

took a few seconds to solve most of the numbers less than 50,000,000. However, took around 6:32 seconds to solve 100,000,000. took around 47 mins to solve 500,000,000. and crashed 11 hours after solving 1.5 billions.

MY CODE:

private static void GetPrimeNumbers(int max)
    {
        var allPossibleNumbers = Enumerable.Range(3, max-3);
        var possiblePrime = allPossibleNumbers
                .AsParallel()
                .Where(n => Enumerable.Range(2, (int)Math.Sqrt(n))
                                      .All(i => n % i != 0)
                )
                ;
        possiblePrime
                .ToArray()
                .AsParallel()
                .Dump()
                ;
    }