The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

This is another brute force attack. I am sure there is a more efficient algorithm, but the following gets it done.

function isPrimeWhile this generates the correct answer, it is painfully slow. I will be revisiting this one in an attempt to optimize it.

{

param ($number)

$isPrime = $true

if($number -lt 2) { $isPrime = $False}

if($number -gt 2 -and $number%2 -eq 0) {$isPrime = $False}

for($i=3;$i -le [math]::Sqrt($number);$i+=2)

{

if($number % $i -eq 0) { $isPrime = $False}

}

$isPrime

}

$limit=2000000

$sum=2

for($i=3; $i -lt $limit;$i+=2)

{

if( isPrime $i)

{

$sum+=$i

"{0}`t{1}" -f $i, $sum

}

}

## 1 comment:

Thank you so much for this post, it was very insightful!

Reverse Phone Lookupalso contains hundreds of cell phone and telecommunications articles and resources covering all aspects of cell phone safety, security, accessories and shopping.Phone Number Trace Reverse Phone Lookup, Unknown Number Search - welcome to reverse-phone-lookup-online.info visit More Reviews.

Post a Comment