Monday, December 29, 2008

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;


Doug said...

Good stuff. Another way to skin the cat.

1..999 |
Where {$_ % 3 -eq 0 -or $_ % 5 -eq 0} |
ForEach {$sum=0} {$sum+=$_} {$sum}

Wes Stahler said...

Gotta love the one-liners! Thanks Doug!

Anonymous said...

Rip Blu-ray for Mac with Blu-ray Ripper for Mac, snow Leopard included.- Free Download and have a try.
Ultimate Blu-ray video converter is actually an 3-in-one software including Blu-ray Ripper, DVD Ripper and Video Converter.
Blu-ray ripper enables you directly rip and convert your Blu-ray movies and general DVDs, even the protected DVDs and Blu-ray DVDs into other popular audio and video formats.