PDA

View Full Version : [PHP] Solving Euler Project #2

Moonbat
07-13-2008, 07:01 PM

The Problem - "Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million."

The Solution :)

<?php

/*********************************
* The Euler Project #2 Solution *
* Coded By Moonbat *
* July 13, 2008 *
*********************************/
\$first = 0; // Here are the two numbers that...
\$wing = array(); // Keeping track of the even-numbered Fibonacci numbers
for (\$i=0; \$i<=31; \$i++) {
\$fibonacci = \$first + \$second; // 0+1, 1+1, 1+2, 2+3
\$first = \$second; // 0-->1, 1-->1, 1-->2, 2-->3
\$second = \$fibonacci; // 1-->2, 2-->3, 3-->5, 5-->8
echo "\$fibonacci <br>";
if ((\$fibonacci &#37; 2) == 0) { // If the current Fibonacci number is even
\$wing[\$i] = \$fibonacci;
} else {
echo "";
}
}
\$arraysum = array_sum(\$wing); // Add 'em all up!
echo "<hr>" . \$arraysum;

?>

Moonbat
07-24-2008, 04:36 PM
After doing a lot of other stuff (using programming and my knowledge of Fibanocci numbers alone) I got to the final stopping point of 31. Since this was my final solution (lolololol insart hitler joke hear) I wanted to make it not have to do more than was needed. I guess I should've done what you did and kept track of the current Fibanocci number to make sure it wasn't over 4,000,000.