Fibonacci problem

Discussion in 'Perl' started by Brett Trost, Jan 22, 2004.

  1. Brett Trost

    Brett Trost Guest

    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.
    Brett Trost, Jan 22, 2004
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Jan 22, 2004
    #2
    1. Advertising

  3. Brett Trost

    Guest

    (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.
    , Jan 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fighterman19
    Replies:
    11
    Views:
    848
    Karl Heinz Buchegger
    Sep 8, 2003
  2. Fibonacci numbers

    , Oct 10, 2003, in forum: C++
    Replies:
    28
    Views:
    1,382
    yousafzai
    Jun 5, 2011
  3. Alex Vinokur
    Replies:
    0
    Views:
    469
    Alex Vinokur
    Oct 29, 2003
  4. Lance

    STL for Fibonacci Heap??

    Lance, Dec 1, 2003, in forum: C++
    Replies:
    5
    Views:
    1,350
    Dietmar Kuehl
    Dec 2, 2003
  5. Alex Vinokur
    Replies:
    0
    Views:
    435
    Alex Vinokur
    Jul 26, 2004
Loading...

Share This Page