473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Fibonacci problem

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.
Jul 19 '05 #1
2 7663
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
Jul 19 '05 #2
bt****@shaw.ca (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.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers,...
0
by: Alex Vinokur | last post by:
An algorithm which computes very long Fibonacci numbers http://groups.google.com/groups?selm=bnni5p%2412i47o%241%40ID-79865.news.uni-berlin.de was used as a performance testsuite to compare speed...
4
by: YS Sze | last post by:
If you know the exact longitude and latitude for a specific location, would anyone think it'd make any sense to find out if this set of location numbers is really part of the Fibonacci series or...
11
by: MARQUITOS51 | last post by:
Hey guys this is the fibonacci series. But Im having this huge problem. First of all I want to do the simplest code possible so then I can use user defined functions and arrays. But this one didnt...
14
by: felixnielsen | last post by:
Im actually kinda embarassed to ask this question... @code start #include <iostream> int main() { unsigned long long a = 1; unsigned long long b = 1; for (int i = 0; i < 45; i++) { a += b;...
8
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For...
3
by: greek | last post by:
Hi! I hav to generate fibonaaci series using recursion: 0,1,1,2,3,5,8,18,21... whr fibonacci(0)=0 fibonacci(1)=1 fibonacci(n)=fibonacci(n-1)+fibonacci(n-2) ive witten the code but having 2...
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
6
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.