Results 1 to 7 of 7
  1. #1
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default need some help doing loops with fibonacci sequence

    a bit confused here. i set one variable to 0 and the other to 1. I then ask the user how many elements they wish to only count up to. i guess the way this problem was written up is confusing me even more. im trying to figure out how to finish this. basically if the user only wants the first 5 numbers of the sequence, i then need to print it out. i was going with a for(x,y,z++) but im not sure if it is appropriate in this case. any help would be appreciated

    Java Code:
    import java.util.Scanner;
    public class FibonacciSequence {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner input= new Scanner(System.in);
    		long prevNum=0;
    		long newNum=1;
    		System.out.print( "Enter number of elements: ");
    		int element= input.nextInt();
    		System.out.println(prevNum + " " + newNum +" " + element);
    		for(int i=0;i<=element;i++){
    			long tempVal

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: need some help doing loops with fibonacci sequence

    The first question I might ask is whether you understand what a Fibonacci sequence is and how to construct one on paper.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: need some help doing loops with fibonacci sequence

    Quote Originally Posted by jim829 View Post
    The first question I might ask is whether you understand what a Fibonacci sequence is and how to construct one on paper.

    Regards,
    Jim
    i know it goes 0 1 1 2 3,etc. adds the previous two numbers.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: need some help doing loops with fibonacci sequence

    Exactly! So you need to put that inside a loop. The currentValue is the sum of the previous two. Then you need to update the previous two.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: need some help doing loops with fibonacci sequence

    sorry jim, you lost me lol

    im supposed to have the user choose how long this sequence goes to. if he says he only wants the first 5 numbers, then thats how far itll be. Does that go inside the for loop?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: need some help doing loops with fibonacci sequence

    The first five numbers just means the first five of the sequence (at least that's my interpretation). But the sequence always starts out with 0 and 1. So the first five would be 0 1 1 2 3. The first eight would be 0 1 1 2 3 5 8 13. The hard part is figuring out how to update the variables inside the loop to continue calculating the next value in the series. It helps to try this on paper with some variables.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: need some help doing loops with fibonacci sequence

    ok i just had it clarified. what i didnt understood was i needed a tempValue.

    so tempVal=newElement;
    newElement=prevElement+ newElement;
    prevElement=tempVal;

    i was confused if that was going into the loop or not.

Similar Threads

  1. Replies: 1
    Last Post: 01-23-2013, 06:06 PM
  2. Fibonacci Sequence Problem
    By Zigster in forum New To Java
    Replies: 10
    Last Post: 07-06-2012, 09:54 PM
  3. Replies: 8
    Last Post: 03-10-2011, 03:16 AM
  4. Fibonacci sequence using iterative and recursive method
    By baumboards in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 06:57 PM
  5. Fibonacci sequence
    By ŖàΫ ỏƒ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 06:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •