Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org need perl help calculating fibonacci numbers
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 12-16-2003, 08:22 PM #1 WorldBuilder Member Contributing Member   Registered: Mar 2003 Location: Massachusetts, USA Distribution: RH 8, Solaris, Windoze eXPunged Posts: 520 Rep: need perl help calculating fibonacci numbers Hi all, I have a script that I am trying to get to write fibonacci numbers. Here's what I need to do: Code: ```Write a program that prints out the first n fibonacci series numbers. *The fibonacci series start out like this: n=0 n=1 n=2 n=3 n=4 n=5 n=6 n=... 1 1 2 3 5 8 13 ... In math terms, each element is the sum of the preceding two elements: F(0)=1, F(1)=1, F(n) = F(n-1) + F(n-2) for n>1``` Here's how I wrote it so far: Code: ```#! /usr/bin/perl print "Please type in how many fibonacci numbers you would like displayed. n: "; \$n = ; \$x = 1; \$y = 0; \$a = 0; while (\$a <= \$n) { process(\$x, \$y, \$a); \$a++; } sub process { \$z = \$_[0] + \$_[1]; \$y = \$_[0]; \$x = \$z; print "n=\$_[2]\t\$z\n"; }``` The output of this script if I input n equal to 5 is: Code: ```n=0 1 n=1 2 n=2 3 n=3 5 n=4 8 n=5 13``` Obviously, that is not right. What am I doing wrong?! Thanks!
 12-16-2003, 09:34 PM #2 AltF4 Member   Registered: Sep 2002 Location: .at Distribution: SuSE, Knoppix Posts: 532 Rep: you need to init fib(0)=1 and fib(1)=1 first the program should start at fib(2) .. #! /usr/bin/perl print "Please type in how many fibonacci numbers you would like displayed. n: "; \$n = ; \$x = 1; \$y = 1; \$a = 2; print "n=0\t1\n"; print "n=1\t1\n"; while (\$a <= \$n) { process(\$x, \$y, \$a); \$a++; } sub process { \$z = \$_[0] + \$_[1]; \$y = \$_[0]; \$x = \$z; print "n=\$_[2]\t\$z\n"; }
 12-16-2003, 09:44 PM #3 WorldBuilder Member Contributing Member   Registered: Mar 2003 Location: Massachusetts, USA Distribution: RH 8, Solaris, Windoze eXPunged Posts: 520 Original Poster Rep: Thanks! You're absolutely right. I didn't even see that. I appreciate it, bro!
 12-16-2003, 09:59 PM #4 AltF4 Member   Registered: Sep 2002 Location: .at Distribution: SuSE, Knoppix Posts: 532 Rep: no problem - you're welcome
 12-16-2003, 10:25 PM #5 ToniT Senior Member   Registered: Oct 2003 Location: Zurich, Switzerland Distribution: Debian/unstable Posts: 1,357 Rep: Ther's more than one way to do it. An example impementation with easy possibility for precalculating the values. Code: ```#! /usr/bin/perl @values = (1,1); sub calculateOneMore { push @values,\$values[\$#values-1]+\$values[\$#values-2]; } sub Next { calculateOneMore(); shift @values; } print "n=\$_\t" . Next() . "\n" foreach (0 .. shift);```
 12-17-2003, 01:41 AM #6 Berng Member   Registered: Dec 2003 Location: Siberia,Russia Distribution: Basically RedHat Posts: 34 Rep: But, of couse the simpliest way to correct this program is to swap the first \$x, \$y values(5-6 lines): instead of \$x = 1; \$y = 0; put \$x = 0; \$y = 1; Best Regards, Oleg. Last edited by Berng; 12-17-2003 at 01:42 AM.