Fibonacci Series in 1-Line PHP / Java Software

By Angsuman Chakraborty, Gaea News Network
Saturday, 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.

Filed under: Headline News, Java Software, PHP, Tech Note

Tags:
Discussion

rosh
June 19, 2008: 6:40 am

does anyone know the fibonacci code for this output?
0 1 1 2 3 5 8

i only know this part $limit=7 =( help


Rich G
February 12, 2008: 8:12 am

That is not the fibonacci sequence..

October 4, 2006: 5:34 am

Single quotes problem mate. It now works fine.


hidden
October 4, 2006: 2:12 am

Your php fubonacci script does not work mate

May 27, 2006: 6:40 am

How is it relevant in context?


prabhu
May 24, 2006: 12:32 am

all the viruses are not dangerous some are operating systems

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :