Fibonacci Series in 1-Line PHP / Java Software
By Angsuman Chakraborty, Gaea News NetworkSaturday, October 22, 2005
No, I am not competing for obfuscated code context. I realized while showing it to someone that Fibonacci series can be written much more succintly using modern programming languages like c, java or php.
Here is the one-line php version:
<?php for($current = 1, $last = 0, $i = 1;$i <= 100;$i++) echo ($current = $last + ($last = $current)).'<br/>' ?>
It outputs 100 fibonacci numbers, each in separate line.
Note: It doesn’t use a temporary variable as is normal in other implementations. It obviates the need by utilizing the power of expressions. The rest is achieved with a simple for loop.
Here is the same example in Java / JSP.
<% for(double current = 1, last = 0, i = 1;i++ <= 100;) out.println((current = last + (last = current)) + "<br/>"); %>
Note: The double is used because long is unable to display correctly all 100 numbers due to size limitations.
June 19, 2008: 6:40 am
does anyone know the fibonacci code for this output? i only know this part $limit=7 =( help |
Rich G |
hidden |
prabhu |
rosh