Results 1 to 20 of 24
Thread: Need help with java question
- 05-17-2010, 06:23 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Need help with java question
Hi all,
Can anybody resolve this question.Thank you in advance.I need help plz.
In mathematics, there is a famous sequence of numbers called the Fibonacci
sequence after the thirteenth-century Italian mathematician Leonardo Fibonacci. The
first two terms in this sequence are 0 and 1, and every subsequent term is the sum of
the preceding two. Thus the first several numbers in the Fibonacci sequence are as
follows:
F0 = 0
F1 = 1
F2 = 1 (0 + 1)
F3 = 2 (1 + 1)
F4 = 3 (1 + 2)
F5 = 5 (2 + 3)
F6 = 8 (3 + 5)
Write a program to display the values in this sequence from F0 through F15.
- 05-17-2010, 06:36 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
You'll get better help if you do more than simply post your homework assignment without asking a question. To help folks know where you're stuck, please post what you've tried so far and any specific questions. The more specific the question, the more helpful the answer.
- 05-17-2010, 08:41 PM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 11
that's easy enough:
Java Code:System.out.println("0"); System.out.println("1"); System.out.println("1"); System.out.println("2"); System.out.println("3"); System.out.println("5"); System.out.println("8"); // keep going until you have 15 printouts
- 05-17-2010, 08:44 PM #4
- 05-17-2010, 09:16 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 11
this is how you do it with loops:
Java Code:String [] fibonacci = {"0", "1", "1", "2", "3", "5", "8", }; //continue until at least 100 strings for (int i = 0; i < requestedNumber; i ++) { System.out.println (fibonacci [i]); }
PS. what particularly cracks me up is the shear wealth of information you get by googling "java fibonacci"...
- 05-17-2010, 09:19 PM #6
I believe there is one more way you can do this with a for loop and two incrementing integers. I'm at work, Who can do it??
:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
- 05-17-2010, 10:12 PM #7
Whoops almost did it
- 05-18-2010, 05:22 PM #8
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Mine doesn't work but this what I have done so far.
/*File:chapt4ques9.java
* This program displays sequence of numbers called the Fibonacci
sequence
* */
import acm.program.*;
public class chapt4ques9 extends ConsoleProgram{
public void run(){
int sum=0;
for (int x= 0; x <=1; x++){
for (int i =0; i <15; i++)
println("F"+x+"="+i);
}
}
}
- 05-18-2010, 05:26 PM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 11
I'll give you a big hint. Using recursion is a pretty logical way of calculating the fibonacci sequence. I'll even give a "fill in the blanks" type help:
Java Code:int fib(int n) { if(n //what goes here) return n; return fib(//hmm)+fib(//hmm indeed); }
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-18-2010, 05:27 PM #10
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
I tried above code iluxa but it didn't work for me.
- 05-18-2010, 05:31 PM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 11
- 05-18-2010, 05:33 PM #12
Now you need to design the logic to compute the series. This is done with paper and pencil.
Looking at what you want for output,i t appears that F0 and F1 are the initial input
then F2 = F0 + F1
and F3 = F2 + F1
Then replace the fixed subscripst with new names:
Fnew = FOld + FOld-1
then move the values thru the variables. Ie Fold = Fnew and FOld-1 = FOld
F0 = 0
F1 = 1
F2 = 1 (0 + 1)
F3 = 2 (1 + 1)
Why do you have nested if statements in your program segment?
What does your program print out?
- 05-18-2010, 05:56 PM #13
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Sorry Moonchile I meant to say I tried iluxa's code but didn't work form.We haven't studied recursion on java yet.Hence It sounds advance for me.Is there other way using if,while,etc loops we can resolve it.
- 05-18-2010, 06:01 PM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 11
What I meant was that the code Iluxa posted was "joke code". It's not a solution, if you look at it, it doesn't do any calculations, just prints out preset strings. If recursion is too advanced for you, there were other responses in this thread that give you hints at solving this problem.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-18-2010, 06:11 PM #15
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Hi Norm,
My program displays:
- 05-18-2010, 06:14 PM #16
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
okay moonchile now i got it.lol
- 05-18-2010, 06:22 PM #17
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Hi Norm,
I am very weak in java.Could you write the code for me plz?
- 05-18-2010, 06:59 PM #18
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
- 05-18-2010, 07:09 PM #19
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Thank you for the tuff love.I found a solution on the google.:)
- 05-18-2010, 07:13 PM #20
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 11
Similar Threads
-
Java Question :D
By thisisIT in forum New To JavaReplies: 6Last Post: 03-12-2010, 05:04 PM -
Java question
By TGH in forum New To JavaReplies: 12Last Post: 11-27-2009, 03:05 PM -
question about java rmi
By hakimade in forum Advanced JavaReplies: 1Last Post: 07-01-2009, 08:15 AM -
Java Question
By Jay-1.1 in forum New To JavaReplies: 11Last Post: 05-01-2008, 05:04 PM -
Java Question, i need to be answered
By Sunshine in forum New To JavaReplies: 7Last Post: 04-28-2008, 02:00 PM
Bookmarks