Fibonacci in Java code

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

    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        for (int i = 1; i <= N; i++)
            System.out.println(i + ": " + fib(i));
    }

}

Suppose the user enters "java Fibonacci 7", so the result would be: 1: 1

2: 1

3: 2

4: 3

5: 5

6: 8

7: 13

I seem to be completely confused about how this works. Starting from number 3. When the fib (i) method passes 3, it also should not return 3, since if n = 3, then the sum fib (n-1) / , which is 2 / and fib (n-2) / which 1 / is equal to 3. And so on, the remaining numbers forward.

+4
source share
17 answers

If you pass 3 to your function, it will do the following:

fib(3) = fib(2) + fib(1) //so we we are into the else statement, because 3 > 1
= fib(2) + 1             //fib(1) = 1 because 1 <= 1 so you return it (if statement)
= (fib(1) + fib(0)) + 1  //2 > 1 => we go to the else statement
= (1 + 0) + 1            //0 <= 1 & 1 <= 1 so we are into the if and return the values 
= 2
+3
source

This is a much simpler code to generate a Fibonacci sequence, such as '0 1 1 2 3 ...'.

public static void main (String[] args) {
    int f = 0;
    int g = 1;

    for (int i = 1; i <= 10; i++) {
        System.out.print(f + " ");
        f = f + g;
        g = f - g;
    } 

    System.out.println();
}
+17
                           F(n)
                            /    \
                        F(n-1)   F(n-2)
                        /   \     /      \
                    F(n-2) F(n-3) F(n-3)  F(n-4)
                   /    \
                 F(n-3) F(n-4)

, ! , , . , F (n-3) 3 .

. dasgupta 0.2

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

public static void main(String[] args){
    Integer c = 4;
    Integer answer = f(c);
    System.out.println("Fibonacci " + c + " number is: " + answer);
}
+2

!

( ):

    int fib( int x ) {
        return x > 1 ? fib(x - 1) + fib(x - 2) : x; }

, :

        ///fast version fibbonacci sequence.
        public static float fibonacci(int x){
           float[] sequence = new float[x];
           sequence[0] = 1;
           sequence[1] = 1;
          if (x > 1){
             for (int i = 2; i < x; i++){
               sequence[i] = sequence[i-1] + sequence[i-2];
             }
          }
          for (float z : sequence){
              System.out.print("{ " + z + "}, ");
          }
          return sequence[x-1];
        }
+2

Java 8 Stream:

public class Main {
    public static void main(String[] args) {
        final int n = 7;
        Fibonacci fibonacci = new Fibonacci();
        Stream.generate(fibonacci::next)
                .limit(n)
                .forEach(System.out::println);
    }
}

public class Fibonacci {
    private long next = 1;
    private long current = 1;
    private int count = 1;

    public FibonacciNumber next() {
        FibonacciNumber fibonacciNumber = new FibonacciNumber(count++, current);
        long previous = current;
        current = next;
        next = current + previous;
        return fibonacciNumber;
    }
}

public class FibonacciNumber {
    private final int count;
    private final long value;

    public FibonacciNumber(int count, long value) {
        this.count = count;
        this.value = value;
    }

    @Override
    public String toString() {
        return count + ": " + value;
    }
}
+2

. , .

public class FinnonnacciDemo2 {

    static int no = 0, n = 8;

    public static void main(String[] args) {
        // This will print series till 8
        fib(0, 1);
    }

    public static void fib(int a, int b) {
        // Terminating condition.
        if (a >= n) {
            return;
        }

        else {
            System.out.print("\t" + no);
            no = a + b;
            a = b;
            b = no;
            fib(a, b);
        }
    }
} 
+1

, (n) . fib (n = 3) = fib (n-1 = 2) + fib (n-2 = 1) = 1 + 1 = 2

0

, 1, 3 .

1 1 2 3 5...

0 1 1 2 3 5 8...

, , 0, fib (0) = 0, fib (1) = fib (2) = 1, : http://oeis.org/A000045

0

... fib(3)=fib(3-1)+fib(3-2)= fib(2)+fib(1)=(fib(2-1)+fib(1-1))+fib(1)= fib(1)+fib(0)+fib(1)= 1 + 0 + 1

0

- . 0 1 1 2 3 5 8... . , - -

fib(3) = fib(2) + fib(1)
       = fib(1) + fib(0) + 1 //fib(1) = 1
       = 1 + 0 + 1 //fib(0) = 0
       = 2

, , , , .

0
import java.math.BigDecimal;

public abstract class Fibonacci {

    public static void main(String[] args) {
        Integer max = Integer.valueOf(args[0]);
        BigDecimal[] fibs = { new BigDecimal(0), new BigDecimal(1) };
        BigDecimal current;
        System.out.print("1 ");
        for (int i = 0; i < max + 1; i++) {
            current = fibs[1].add(fibs[0]);
            System.out.print(current + " ");
            fibs[0] = fibs[1];
            fibs[1] = current;
        }
    }
}
0

n.

 public static long getFibonacci(int n) {
            List<Long> fibonacciNumbers = new ArrayList();
            long j = 0;
            int k = 0;
            for (int i = 0; i <= n; i++) {
                System.out.println("i = " + i);
                if (i == 2) {
                    j += (i - 2);
                } else if (i > 2) {
                    j = fibonacciNumbers.get(i - 1) + fibonacciNumbers.get(k - 2);
                } else {
                    j += i;
                }
                fibonacciNumbers.add(j);
                k++;
                System.out.println("Fibonacci Array = " + Arrays.toString(fibonacciNumbers.toArray()));
            }
            return fibonacciNumbers.get(fibonacciNumbers.size() - 1);
        } 
0

:

private static int fibonacci(int n) {
    if (n == 1) {
        return 1;
    } else if (n == 2) {
        return 1;
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}
0

BigInteger.

   import java.math.BigInteger;
   import java.util.HashMap;
   import java.util.Map;
   import java.util.Scanner;

/**
 * Topic: Dynamic Programming
 * Algorithm: Fib Algorithm Implementation
 *
 * Author: Chris M. Perez Santiago
 * Date: 4 / 15 / 2018
 **/


  public class Main {
     private static BigInteger[] b = new BigInteger[100000001];
     private static Scanner console = new Scanner(System.in);

  public static void main(String[] args) {
     long n = console.nextLong();
     for(long i=1;i<=n;i++) System.out.println(initFib(i));
  }

  private static BigInteger dpFib(Map<Long , BigInteger> map , long n){
     if(map.containsKey(n)) return map.get(n);

     map.put(n - 1 , dpFib(map , n - 1));
     map.put(n - 2 , dpFib(map , n - 2));
     BigInteger sum = map.get(n - 1).add(map.get(n - 2));
     map.put(n , sum);
     return sum;
  }

  private static BigInteger initFib(long n){
     if(BigInteger.valueOf(n).equals(BigInteger.ONE) || BigInteger.valueOf(n).equals(BigInteger.ZERO))
       return BigInteger.ONE;
     Map<Long , BigInteger> map = new HashMap<>();
     map.put(1L , BigInteger.ONE);
     map.put(2L , BigInteger.ONE);
     return dpFib(map , n);
  }
}
0
//Big O notation O(1)
    private void febonacciSeries(int length) {
            ArrayList<Long> fobannachiSeries = new ArrayList<Long>();
            System.out.println("Print Fabonacci Series : ");
            for(int i=0; i<length;i++){
                if(i==0){
                    fobannachiSeries.add(0L);
                }else if(i==1){
                    fobannachiSeries.add(1L);
                }else{
                    fobannachiSeries.add(fobannachiSeries.get(i-1) + fobannachiSeries.get(i-2));
                }
                System.out.print(fobannachiSeries.get(i) + " ");
            }       
    }
0
import java.util.Scanner;

public class Fibonacci2 {

    public static void main(String[]args){

        int a;
        try (Scanner sc = new Scanner(System.in)) {
            System.out.print("Number of Fibonacci numbers to print: ");
            a = sc.nextInt();
            sc.close();
        }
        int c=1; /*c current number b last number*/
        int b=0;
        System.out.println(b);
        System.out.println(c);
        int bb;
        for (int z = 2; z < a ; z++){
        bb = b; 
        b = c;
        c = bb + b; /*bb last last number*/
        System.out.println(z);

    }
    }
}
-2

All Articles