Sunday, February 23, 2014

Perl solution to Project Euler's problem #2

http://projecteuler.net/problem=2
#!/usr/local/bin/perl
$fib1 = 1;
$fib2 = 2;
$sum = 1;
while ($fib2 < 4000000) {
  if($fib2 % 2 == 0){
    $sum += $fib2;
  }
  $aux = $fib2;
  $fib2 += $fib1;
  $fib1 = $aux;
}

print $sum;

Perl solution to Project Euler's problem #1

#!/usr/local/bin/perl
$sum = 0;
for $i (1 .. 999) {
   if(($i % 3) == 0){
      $sum += $i;
   }
   else 
   {
    if(($i % 5) == 0){
    $sum += $i;
    }
   }
}

print $sum;

https://ideone.com/5NrFot