Project Euler

I started doing the project Euler exercises as a test of my algorithms. I will be posting here my results.

 

  • sum the numbers betwwen ]0 , 1000[ that are multiples of 3 or 5
my $i =0;
my $total = 0;
while ($i <1000)
{
 if (($i % 3 == 0) || ($i % 5 == 0))
 {
 $total += $i;
 }
 $i++;
}
print $total;
  • sum the even numbers in the fibonacci sequence that do not exceed 4’000’000

 

use Data::Dumper;
my @numbers = (1,2);
my $total = 0;
my $temp = 0;
while ($temp < 4000000)
{
$temp = $numbers[-1] + $numbers[-2];
push @numbers, $temp;
}
foreach my $tmp (@numbers)
{
 $total += $tmp if $tmp % 2 == 0; 
}
print Dumper $total;

One thought on “Project Euler

Comments are closed.