Results 1 to 5 of 5
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default Can somebody explain me this plz

    Hi,
    I have resolved my task by researching on the Internet but I am having trouble how this mathematics is working.

    question is

    9. 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.


    answer is :

    -----------------------------------------------xx-------------------
    import acm.program.*;


    public class fibbonaci extends ConsoleProgram {
    public static int fib(int n) {
    int prev1=0, prev2=1;
    for(int i=0; i<n; i++) {
    int savePrev1 = prev1;
    prev1 = prev2;
    prev2 = savePrev1 + prev2;
    }
    return prev1;
    }

    public void run() {
    for (int i=0; i<=15; i++)

    println("F"+i+"="+fib(i)+", ");

    }

    }


    Can somebody explain me this bit plz:
    public static int fib(int n) {
    int prev1=0, prev2=1;
    for(int i=0; i<n; i++) {
    int savePrev1 = prev1;
    prev1 = prev2;
    prev2 = savePrev1 + prev2;
    }
    return prev1;




    thanks alot dudes.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You explained the piece of code in question by yourself:
    Quote Originally Posted by ccie007 View Post
    9. 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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Can somebody explain me this bit.

    public static int fib(int n) {
    int prev1=0, prev2=1;
    for(int i=0; i<n; i++) {
    int savePrev1 = prev1;
    prev1 = prev2;
    prev2 = savePrev1 + prev2;
    }
    return prev1;

  4. #4
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    by lines
    1.its a method, which bring in a variable integer n (ex. n could = 5)
    2.for integer i is set to 0, for i is less than n, than increment i by 1 for each loop
    3. integer saveprev is now the value of prev 1
    4. prev1 is now the value of prev2
    5. prev 2 is now the value of saveprev1 and prev2 added together
    6. end for loop
    7. return the value of prev1 to the place where the fib method was called

    so in this for loop
    becomes saveprev1 = 0
    prev1 becomes 1
    prev2 become 0+1
    return prev1
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    As I said, your first post contained the explanation of the method you were asking about. Just reposting the question does not make it valid again. Do some work on your own, it'll do you good in the long run.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Can someone explain why the red text?
    By 357mag in forum JCreator
    Replies: 0
    Last Post: 05-08-2010, 04:29 AM
  2. Help explain the FOR lines please
    By hydride in forum New To Java
    Replies: 2
    Last Post: 01-19-2010, 11:52 PM
  3. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 06:59 AM
  4. Please explain Java
    By MarkWilson in forum New To Java
    Replies: 7
    Last Post: 07-02-2008, 08:38 AM
  5. need to explain this code
    By reached in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 10:01 PM

Posting Permissions

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