Someone I'm following on Twitter posted a link to an interesting site called
Project Euler. It's full of interesting maths puzzles which are designed in such a way that you should create a program to calculate the result for you.
I like the look of the puzzles on the site and they remind me a little of the ones I've done in the past on the
Facebook Puzzles page.
I've decided to start working my way through them when I get spare time and I'll be trying to work out PHP solutions to the problems and will post them here.
The details for problem 1 are below:
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.
The program was pretty simple, just work up from 1 to 999 and if the number is divisible by 3 or 5 add it to the total and output:
<?
/******************************************************************
* ProjectEuler.net * Problem 1 * 2010-05-31 * Martin Caine *
******************************************************************/
$total = 0;
// check $i between 1 and 999
for( $i=1; $i<1000; $i++ )
{
// if $i divides by 3 or 5 add to the total
if( ($i/3)==(int)($i/3) || ($i/5)==(int)($i/5) )
{
$total += $i;
}
}
echo $total;
?>
If you found this post helpful please leave a comment below: