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. Code:
#! /usr/bin/perl Code:
n=0 1 |
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 = <STDIN>; $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"; } |
Thanks! You're absolutely right. I didn't even see that. I appreciate it, bro!
|
no problem - you're welcome
|
Ther's more than one way to do it.
An example impementation with easy possibility for precalculating the values. Code:
#! /usr/bin/perl |
:D
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; :D :D :D Best Regards, Oleg. |
All times are GMT -5. The time now is 08:24 PM. |