Welcome to Dream.In.Code
Become a Java Expert!

Join 150,030 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,584 people online right now. Registration is fast and FREE... Join Now!




Fibonacci numbers

 
Reply to this topicStart new topic

Fibonacci numbers, Java

moetima
11 Jun, 2008 - 01:13 PM
Post #1

New D.I.C Head
*

Joined: 11 Jun, 2008
Posts: 4

hi can u help with writing a java application that uses recursion to print out a list of the first 25 Fibonacci numbers
User is offlineProfile CardPM
+Quote Post

pbl
RE: Fibonacci Numbers
11 Jun, 2008 - 01:46 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(moetima @ 11 Jun, 2008 - 02:13 PM) *

hi can u help with writing a java application that uses recursion to print out a list of the first 25 Fibonacci numbers

Search this forum and the Code Snippets there are dozen of examples
User is online!Profile CardPM
+Quote Post

Locke37
RE: Fibonacci Numbers
11 Jun, 2008 - 06:57 PM
Post #3

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.

___________________________________________________________________

But yeah, there are numerous snippets and such for that.

One word of advice though.

DON'T USE RECURSION FOR THE FIBONACCI SEQUENCE UNLESS YOU ARE INSTRUCTED TO DO SO...

The reason I say this is because it has 2 recursive calls in each respective call. The execution time increases EXPONENTIALLY. I believe the time it takes to find the 50th number in the sequence takes about 20 minutes if I remember rightly, but it's been a while since that chapter in my class. I DO remember though, that it's EXTREMELY discouraged to use recursion for finding these numbers.

This post has been edited by Locke37: 11 Jun, 2008 - 07:04 PM
User is offlineProfile CardPM
+Quote Post

cutegrrl
RE: Fibonacci Numbers
11 Jun, 2008 - 08:58 PM
Post #4

D.I.C Head
**

Joined: 12 May, 2008
Posts: 72



Thanked: 7 times
My Contributions
java
public class Fibonacci {

// Program execution starts here
public static void main(String[] args) {
// Print first 25 numbers of Fibonacci sequence
for (int i=0; i<=25; i++) {
System.out.print(fib(i)+", ");
}
}

public static int fib(int n) {
if (n < 2) {
return n;
}else {
return fib(n-1)+fib(n-2);
}
}

} // end of class Fibonacci


This post has been edited by cutegrrl: 11 Jun, 2008 - 08:59 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Fibonacci Numbers
11 Jun, 2008 - 09:20 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(cutegrrl @ 11 Jun, 2008 - 09:58 PM) *

java
public class Fibonacci {

// Program execution starts here
public static void main(String[] args) {
// Print first 25 numbers of Fibonacci sequence
for (int i=0; i<=25; i++) {
System.out.print(fib(i)+", ");
}
}

public static int fib(int n) {
if (n < 2) {
return n;
}else {
return fib(n-1)+fib(n-2);
}
}

} // end of class Fibonacci


Cutegrrl your patience will always impressed me smile.gif
User is online!Profile CardPM
+Quote Post

moetima
RE: Fibonacci Numbers
11 Jun, 2008 - 11:18 PM
Post #6

New D.I.C Head
*

Joined: 11 Jun, 2008
Posts: 4

thank you this really helped


QUOTE(Locke37 @ 11 Jun, 2008 - 07:57 PM) *

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.

___________________________________________________________________

But yeah, there are numerous snippets and such for that.

One word of advice though.

DON'T USE RECURSION FOR THE FIBONACCI SEQUENCE UNLESS YOU ARE INSTRUCTED TO DO SO...

The reason I say this is because it has 2 recursive calls in each respective call. The execution time increases EXPONENTIALLY. I believe the time it takes to find the 50th number in the sequence takes about 20 minutes if I remember rightly, but it's been a while since that chapter in my class. I DO remember though, that it's EXTREMELY discouraged to use recursion for finding these numbers.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month