Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Fibonacci problem

Reply
Thread Tools

Fibonacci problem

 
 
Brett Trost
Guest
Posts: n/a
 
      01-22-2004
OK, I wanted to make a recursive fibonacci method in Perl, and I can't
understand why it is not working, especially since I wrote the exact
same thing in Java and it works. Here's the perl code:

#!/usr/bin/perl
sub fib {
$num = shift;
return 0 if $num == 1;
return 1 if $num == 2;
return fib($num - 1) + fib($num - 2);
}

print fib($ARGV[0]);

And here's the Java code:

class fib
{
public static void main(String[] args)
{
System.out.println(fib(Integer.parseInt(args[0])));
}

static int fib(int x) {
if (x == 1) return 0;
if (x == 2) return 1;
return fib(x - 1) + fib(x - 2);
}
}

The perl one keeps on running forever. If I print out $num, it goes
rapidly into the negatives. Why is this, when the base case should
take care of that, and the Java code has the exact same logic and
works?! It must be an error with my Perl, but I can't find it.
Thanks for any help.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      01-22-2004
Brett Trost wrote:
> OK, I wanted to make a recursive fibonacci method in Perl, and I can't
> understand why it is not working, especially since I wrote the exact
> same thing in Java and it works. Here's the perl code:
>
> #!/usr/bin/perl


There is a "use strict;" missing here ...

> sub fib {
> $num = shift;


....which would have told you that $num requires a declaration.
You are using the _global_ variable $num, not a variable that is local to
the sub.

> return 0 if $num == 1;
> return 1 if $num == 2;
> return fib($num - 1) + fib($num - 2);
> }


jue


 
Reply With Quote
 
 
 
 
lesley_b_linux@yahoo.co.yuk
Guest
Posts: n/a
 
      01-22-2004
(Brett Trost) writes:

> OK, I wanted to make a recursive fibonacci method in Perl, and I can't
> understand why it is not working, especially since I wrote the exact
> same thing in Java and it works. Here's the perl code:
>
> #!/usr/bin/perl
> sub fib {
> $num = shift;
> return 0 if $num == 1;
> return 1 if $num == 2;
> return fib($num - 1) + fib($num - 2);
> }
>
> print fib($ARGV[0]);
>

<snip>

> The perl one keeps on running forever. If I print out $num, it goes
> rapidly into the negatives. Why is this, when the base case should
> take care of that, and the Java code has the exact same logic and
> works?! It must be an error with my Perl, but I can't find it.
> Thanks for any help.


I put in

#! /usr/bin/perl -w

at the start and debugged ....
../test.pl 8
Deep recursion on subroutine "main::fib" at ./test.pl line 7.

Adding :

#! /usr/bin/perl -w
use strict;

and using

my $num = shift;

gives the right results.

hth

Lesley

---

email address munged : remove relevant y if nec.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fibonacci numbers dleecurt@cc.usu.edu C++ 28 06-05-2011 04:30 AM
hello! first post to clr. I'm asking about an attempt at a lazy rubysolution to computing fibonacci numbers for a project euler problem. seems tobe a bug in lazy ruby... tphyahoo Ruby 6 08-08-2008 08:15 PM
STL for Fibonacci Heap?? Lance C++ 5 12-02-2003 09:17 AM
Computing Fibonacci numbers as a performance testsuite Alex Vinokur C++ 0 10-29-2003 12:07 PM
How to do fibonacci by linked list ( not by recursive) fighterman19 C++ 11 09-08-2003 02:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57