Topic awaiting preservation: PHP and Big Numbers. (Page 1 of 1) |
|
---|---|
Paranoid (IV) Inmate From: Wales |
posted 05-02-2008 02:27
I'm trying to find the sum of all of the even terms in the Fibonacci series without exceeding four million here's my code. code: <?php $first = 0; $sum = 0; $second = 1; $n = 4000000; for($i=1;$i<=$n-1;$i++) { $final = $first + $second; $first = $second; $second = $final; if(($final % 2) == 0){ $sum = $sum + $final; } } echo $sum; ?>
|
Lunatic (VI) Inmate From: under the bed |
posted 05-02-2008 02:39
Don't know, but - quote:
|
Paranoid (IV) Inmate From: Wales |
posted 05-02-2008 02:49
Yeah I got that, I tracked down the issue, The number int was overflowing, But that was just a sympton of my poor logic. code: <?php $first = 0; $second = 1; $sum = 0; $n = 4000000; while($second < $n){ $final = $first + $second; $first = $second; $second = $final; if(($final % 2) == 0){ $sum = $sum + $final; } } echo $sum; ?>
|
Maniac (V) Mad Scientist From: :morF |
posted 05-02-2008 13:01
Also, Trigger, it's good to note that you can condense (if I recall correctly, that is) "$sum = $sum + $final" into "$sum += $final" |