Monday, December 29, 2008

Project Euler and PowerShell - Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.

What is the 10001^(st) prime number?

function isPrime
{
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
}

$i = $j = 0
do {
if(isPrime $i){$j++;$i}
$i++
}
until ($j -eq 10001)

Project Euler and PowerShell - Problem 6

This was an easy one!
The sum of the squares of the first ten natural numbers is,

1^(2) + 2^(2) + ... + 10^(2) = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^(2) = 55^(2) = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

$sumSquares = $squareSums = $difference = 0
for ($i=1;$i -le 100;$i++)
{
$sumSquares += [Math]::Pow($i,2)
$squareSums += $i
}
$difference = [math]::Pow($squareSums,2) - $sumSquares
$difference

Project Euler and PowerShell - Problem 5

Moving on...

Problem 5: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is divisible with no remainder evenly divisible by all of the numbers from 1 to 20?
function modFunction ($mod)
{
$number = 1
$start = $mod
do {
if($number%$mod -eq 0)
{
$mod--
}
else
{
$number++;
$mod=$start
}
}
until ($mod -eq 1)
$number
}

modFunction 20
While short, this is clearly a brute force approach. Anyone have a more efficient algorithm?

Project Euler and PowerShell - Problem 4

Problem 4: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91X99. Find the largest palindrome made from the product of two 3-digit numbers.

function Reverse-Integer
{
param([string]$str)
for ($i = $str.ToString().Length - 1; $i -ge 0; $i--)
{
$c = $c + ($str.ToString().Substring($i,1))
}
[
int]$c
}

$big = 0
for($x=100;$x -le 999;$x++)
{
for($y=100;$y -le 999;$y++)
{
$xy = $x*$y
$yx = Reverse-Integer $xy
if ($yx -eq $xy)
{
if ($xy -gt $big) {$big=$xy}
}
}
}
$big

This one takes a long time to run. If anyone has any ideas on how to refactor this, please share!

Project Euler and PowerShell - Problem 3

Problem 3 -The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143?
function isPrime
{
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
}

$value=600851475143
$sqrValue =[Math]::Sqrt($value)

for($i=3;$i -le $sqrValue; $i+=2)
{
if($value%$i -eq 0 -and (isPrime $i))
{
$maxPrime = $i
}
}

$maxPrime

Project Euler and PowerShell - Problem 2

This is a continuation of my efforts to use PowerShell to solve some of the problems at Project Euler.

Problem 2: 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, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

function Get-SumFib ($n)
{
$current = $previous = 1
while ($current -lt $n)
{
if($current%2 -eq 0)
{
$sum= $sum + $current
}

$current,$previous = ($current + $previous),$current}
$sum
}
Get-Fib 4000000

Project Euler and PowerShell - Problem 1

Was looking at Pete on Software the other day and saw a reference to Project Euler. What a cool site! Basically, the intent of this site is to use computational/programming skills to solve increasing complex mathematical problems. This seemed like an interesting way to test my PowerShell skills. The next few blog posts will be my attempts at solving a few of these.

- Spoiler Alert -

If you have any interest in solving any of these problems at Project Euler, quit reading this post and immediately pull up your programming editor of choice and have at it.

Problem 1: 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.

$result = 0
for($i = 0; $i -lt 1000;$i++)
{
if ($i % 3 -eq 0 -or $i % 5 -eq 0)
{
$result += $i;
}
}
$result