6

Determining the Fibonacci sequence is easy enough to figure out:

int num = 0;
int num2 = 1;
int loop;
int fibonacci;
System.out.print(num2);
for (loop = 1; loop <= 10; loop ++)
{
    fibonacci = num + num2;
    num = num2;
    num2 = fibonacci;
    System.out.print(" " + fibonacci);
}

My problem lies with trying to pinpoint the value for a specified N. As in, If I want to find the 6th element in the sequence, which is 8, how would I find that number, and only that number?

user207421
  • 305,947
  • 44
  • 307
  • 483
CydonPrax
  • 77
  • 1
  • 1
  • 5
  • 1
    this is most definitely homework... – amphibient Oct 22 '12 at 22:54
  • What do you mean by _"the 6th digit in the sequence"_? Are you concatenating all the fibonacci numbers and then counting digits, as in `112358132134...`? Or do you just want the _nth_ fibonacci number? If instead of _6th_ you wanted the _8th_ "digit", what you be expecting to get as output, `21` or `3`? – Jim Garrison Oct 22 '12 at 23:00
  • 1
    In the sequence, the first digit is 1. The second is 1. The third is 2...etc..the 8th would be 21, 9th 32...If I wanted to find the 6th(which is 8), how would I find it? – CydonPrax Oct 22 '12 at 23:03
  • 3
    It seems you want the nth Fibonacci *number*, not *digit*. – madth3 Oct 22 '12 at 23:23

7 Answers7

9

In your code, num starts as the 0th Fibonacci number, and num1 as the 1st. So to find the nth, you have to iterate the step n times:

for (loop = 0; loop < n; loop ++)
{
    fibonacci = num + num2;
    num = num2;
    num2 = fibonacci;
}
System.out.print(num);

and only print it when you've finished.

When the loop counter loop has the value k, num holds the kth Fibonacci number and num2 the (k+1)th.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • +1 but a couple of quibbles: (1) there is no _zeroth_ Fibonacci number. (2) The Fibonacci sequence can be said to start with the sequence `0,1` or `1,1`; which definition you choose determines which is the _first_ Fibonacci number – Jim Garrison Oct 22 '12 at 23:32
  • 3
    Usually, the n-th Fibonacci number, `F(n)` if you wish, is `(p^n - (1-p)^n)/sqrt(5)` with `p = (1+sqrt(5))/2`. So `F(0)`, the zeroth Fibonacci number is 0. – Daniel Fischer Oct 22 '12 at 23:36
  • As fibonacci numbers grows fast, it is better to use `BigInteger` in calculations – stemm Oct 22 '12 at 23:37
1

To find the n'th digit, we need to know the length of the Fibonacci numbers. You can convert int to string using Java's Integer.toString(int) function. Using the string, one can then determine the length of the converted Fibonacci number.

EDIT: Removed code b/c likely hwk question

ehuang
  • 811
  • 1
  • 8
  • 17
1
int n=5;//position of the fibonacci number to find
int fibonacci=0,num=0,num2=1;
for(int loop=1;loop<n;loop++)
{
   fibonacci=num+num2;
   num=num2;
   num2=fibonacci;
}
System.out.println(num);
shubhamr238
  • 1,226
  • 14
  • 22
0
import java.util.Scanner;
public class fibonacci{
public static void main(String[]args){
    Scanner i=new Scanner(System.in);
    String n=System.getProperty("line.separator");

    int count=0,x=0,y=1,sum;

    System.out.println("Enter a number:  ");
    int n=i.nextInt();

    for(count=0;count<n;count++){
        System.out.print(" "+ x);
        sum=x+y;
        x=y;
        y=sum;
    }
  }
}
Community
  • 1
  • 1
kumiko
  • 1
0

I hope my answer helps. I tried to solve it with the dynamic programming approach. You only need to keep track of two elements till one index before n. When your code is there, the answer will be the summation of elements n-1 and n-2.

public class Fibonacci {

public static void main(String[] args) {
    System.out.println(fib(12));
}

/**
 * Calculate the Fibonacci of n with two int variables and one temp variable. This is using dynamic programming.
 * Time complexity of this approach is O(n). Space complexity of this approach is O(k).
 * 0,1,1,2,3,5,8,13,21,34,55,89,144,...
 *
 *
 * @param n The nth Fibonacci number
 * @return The nth Fibonacci
 */
public static int fib(int n) {
    if (n == 0 || n == 1) {
        return n;
    }
    // Element 1
    int element1 = 0;
    // Element 2
    int element2 = 1;
    // Move the 2 elements window till one index before the nth.
    for (int i = 2; i < n; i++) {
        // Move the 2 elements window forward.
        int temp = element2;
        element2 = element1 + element2;
        element1 = temp;
    }
    // Return the nth Fibonacci by summing Fibonacci n-1 and n-2
    return element1 + element2;
}

}

Mohammad
  • 6,024
  • 3
  • 22
  • 30
0
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    
    int n = s.nextInt();
    int num = 1;
    int num2 = 1;
    int fibonacci;
 
    for (int i=1; i<n; i++) {
        fibonacci = num + num2;
        num = num2;
        num2 = fibonacci;
    }
    System.out.print(num);
}
Chandan Pasunoori
  • 934
  • 1
  • 14
  • 31
-1
import acm.program.*;

public class FibonacciToN extends ConsoleProgram {

    public void run() {

        println("This program will display a table of Fibonacci numbers up to value n.");
        int n = readInt("Enter an integer for value n: ");
        int result = sequence(n);  

    }

    private int sequence(int n) {

        int a = 0;
        int b = 1;

        while (a < n) {  
            println(a); 
            a = a + b;
            b = a - b;
            }

        return a;
    }
}
Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
Colin
  • 1
  • 1