LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 12-16-2003, 08:22 PM   #1
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Rep: Reputation: 30
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 = <STDIN>;
$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!
 
Old 12-16-2003, 09:34 PM   #2
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
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";
}
 
Old 12-16-2003, 09:44 PM   #3
WorldBuilder
Member
 
Registered: Mar 2003
Location: Massachusetts, USA
Distribution: RH 8, Solaris, Windoze eXPunged
Posts: 520

Original Poster
Rep: Reputation: 30
Thanks! You're absolutely right. I didn't even see that. I appreciate it, bro!
 
Old 12-16-2003, 09:59 PM   #4
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
no problem - you're welcome
 
Old 12-16-2003, 10:25 PM   #5
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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);
 
Old 12-17-2003, 01:41 AM   #6
Berng
Member
 
Registered: Dec 2003
Location: Siberia,Russia
Distribution: Basically RedHat
Posts: 34

Rep: Reputation: 15

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating checksum of a structure george_mercury Programming 3 01-09-2005 05:07 AM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM
calculating download time/speed juanb Linux - Networking 4 03-19-2004 02:49 PM
Calculating Surface Distance! john23 Programming 1 11-27-2003 01:45 PM
calculating server load hardigunawan Programming 5 06-27-2003 11:07 AM


All times are GMT -5. The time now is 02:44 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration