[SOLVED] Project Euler Problem #1
In an effort to do more programming, I’ve decided to start doing Project Euler problems. To quote the website:
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.
Last night I solved Problem 1: Add all the natural numbers below one thousand that are multiples of 3 or 5. I used the brute force approach, iterating over each number from 1-999. The first attempt I was adding numbers that were divisible by 15 twice, but I quickly realized my mistake.
Here’s the code in Perl.
#!/usr/bin/perl
use strict;
use warnings;
my ( $index, $sum );
foreach $index ( 1 .. 999 ) {
if ( $index / 3 == int( $index / 3 ) or $index / 5 == int( $index / 5 ) ) {
$sum += $index;
}
}
print "$sum \n";∞ December 22, 2008 at 05:25pm

