ProjectEuler
Thursday, July 7, 2011
<# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. #> # 998001 - so what's the largest palindrome number less than this one - then check if it's a product of 3-digit numbers function ispalindrome { [cmdletbinding()] param( [int] $number ) process{ $digits = @() $next_int = $number while ($next_int -gt 9) { $digit = $next_int % 10 $digits = $digits + $digit $next_int = ($next_int / 10) - (($next_int % 10)/10) #(99099 / 10) - ((99099 % 10)/10) } $digits = $digits + $next_int #$digits #$digits.
Continue reading →
Friday, June 17, 2011
Turns out it was the recursion the factorial function - I’ve reworked it to use a for loop
function factorial { [cmdletbinding()] param($x) if ($x -lt 1) { return "Has to be on a positive integer" } Write-Verbose "Input is $x" $fact = 1 for ($i = $x; $i -igt 0; $i -= 1){ #Write-verbose "i: $i" $fact = [System.Numerics.BigInteger]::Multiply($i , $fact) } Write-Verbose "i equals $i" $fact } it’s still running for factorial 486847…
Continue reading →
Sunday, June 12, 2011
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)!
Continue reading →
Saturday, June 11, 2011
things to do… No more than the square root of the value. test each value? start from the square root and work down. the first one is the largest…
Continue reading →
Saturday, June 11, 2011
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…
Continue reading →
Saturday, June 11, 2011
function factorial { [cmdletbinding()] param([int64] $x) if ($x -lt 1) { return "Has to be on a positive integer" } if ($x -eq 1) { [int64] $x } else { [int64] $x * (factorial ($x-1)) } }
Continue reading →
Friday, April 15, 2011
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
Continue reading →
Wednesday, April 13, 2011
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
Continue reading →