amonkeyseulersolutions:
I’ve read that an integer p > 1 is prime if and only if the factorial (p - 1)! + 1 is divisible by p. So I’ve written this: Read More
Here’s the fixed version…
function isprime { [cmdletbinding()] param($x) if ($x -lt 1) { return "Has to be on a positive integer" } # An integer p > 1 is prime if and only if the factorial (p - 1)!
I’ve read that an integer p > 1 is prime if and only if the factorial (p - 1)! + 1 is divisible by p. So I’ve written this:
function isprime { [cmdletbinding()] param([int] $x) if ($x -lt 1) { return "Has to be on a positive integer" } # An integer p > 1 is prime if and only if the factorial (p - 1)! + 1 is divisible by p [int] $factorial = factorial ($x-1) Write-Verbose "Factorial: $factorial" $remainder = ($factorial + 1) % $x Write-Verbose "Remainder: $remainder" if ($remainder -eq 0) { return $true } else { return $false } } Unfortunately it doesn’t work for some numbers known to be prime - 29 is the example I have…
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
function fib { [cmdletbinding()] param ( [int] $x ) process { if ($x -lt 2) { $x } else { $n1 = $x - 1 $n2 = $x - 2 $fib2 = fib $n2 $fib1 = fib $n1 $result = $fib1 + $fib2 #Write-Verbose $result $result } } } #fib 5 -Verbose Write-Host "___________-------------______________" $ScriptStartTime = date for ($i = 0; $fib -lt 4000000 ; $i+=1) { $fib = fib $i Write-Host -ForegroundColor Magenta "fib = $fib" If ($fib % 2 -eq 0 ) { $sum += $fib Write-Host -ForegroundColor Green "sum = $sum" } } $ScriptEndTime = date $ScriptDuration = $ScriptEndTime - $ScriptStartTime Write-Host "`n Time Taken:" + $ScriptDuration
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
Solution to Euler’s Problem #1 for ($i = 1; $i -lt 1000; $i += 1) {if ( ($i % 3 -eq 0) -or ($i % 5 -eq 0) ) { $count += $i } };$count