About 6 months ago, I was surfing and came across a math/programming site that has fascinated me ever since, Project Euler. They have over 200 problems to solve and continue to add more. The problems start off very easy and get difficult rather quickly. Some of the beginning problems can even be solved using paper and pencil.

If you are just starting to learn computer programming, you should check out this site. The first problem can be solved fairly easily, and I will show you how I did it in perl.

First, let’s look at the problem. They want you to “Find the sum of all the multiples of 3 or 5 below 1000.” What the program will have to do is loop through all whole numbers from 1 to 999. Inside the loop, there is a check to see if the number is a multiple of 3 or 5. If it is, it is added to the sum. After the loop, print the sum to the screen.

Here’s the code:

$sum = 0;

$counter = 1;

while( $counter < 1000 ) {

if ( $counter % 3 == 0 || $counter % 5 == 0 ) {

$sum = $sum + $counter;

}

$counter = $counter + 1;

}

print "The answer is $sumn";

This program, just like every other computer program, uses flow control. Basically flow control tell the computer what to do and when to do it. There are 2 flow control structures here, a while loop and an if statement. Both of these are started by a comparison inside parenteses, then an opening curly brace. They are ended by the closing curly brace.

The while statement should be fairly straightforward, but the if statement is a little complex. The percent(%) sign means modulus(mod for short), which is simply the remainder of division. For example, 4 % 3 is 1, 4 % 5 is 4, and 9 % 3 is 0. The double pipe(||) means OR. When reading line 4, you say “If counter mod 3 equals 0 or counter mod 5 equals 0, then.”

Let’s go line by line.

Line:1 |
$sum = 0; |
Set a variable named “sum” to 0. |

Line:2 |
$counter = 1; |
Set a variable named “counter” to 1. |

Line:3 |
while( $counter < 1000 ) { |
Start a loop and continue the loop **while** counter is less than 1000. |

Line:4 |
if ( $counter % 3 == 0 || $counter % 5 == 0 ) { |
Check to see **if** the remainder of counter divided by 3 is zero OR the remainder of counter divided by 5 is zero. |

Line:5 |
$sum = $sum + $counter; |
Add counter to sum if so. |

Line:6 |
} |
Close the if block. |

Line:7 |
$counter = $counter + 1; |
Add 1 to counter. |

Line:8 |
} |
Close the while loop.
At this point, the program will repeat the check at line 3 if check is true goto line 4. |

Line:9 |
print “The answer is $sumn”; |
Print the answer to the screen. |

You can download perl from perl.com. Once it is installed, you can copy and paste the source code into notepad and save it as euler-1.pl or whatever you want. To run the code, double click on the file.

Play around with the code. Poke it. Prod it. Change the while comparison so it only goes to 10, 100, 100000, etc. Changed the if to check for numbers divisble by 2 and 3. If you mess it up, copy and paste the code back into the file.

If you have any problems, leave a comment.