Java Program Fibonacci Sequence

前端 未结 15 1844
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 03:17

I am writing a \"simple\" program to determine the Nth number in the Fibonacci sequence. Ex: the 7th number in the sequence is: 13. I have finished writing the program, it

相关标签:
15条回答
  • 2020-12-17 03:25

    This is the code in Python, which can easily be converted to C/Java. First one is recursive and second is the iterative solution.

    def fibo(n, i=1, s=1, s_1=0):
        if n <= i: return s
        else: return fibo(n, i+1, s+s_1, s)
    
    
    def fibo_iter_code(n):
        s, s_1 = 1, 0
        for i in range(n-1):
           temp = s
           s, s_1 = s+s_1, temp
           print(s)
    
    0 讨论(0)
  • 2020-12-17 03:27

    Try this example, it calculates the millionth Fibonacci number in a reasonable time frame without any loss of precision.

    import java.math.BigInteger;
    
    /*
    250000th fib # is: 36356117010939561826426 .... 10243516470957309231046875
    Time to compute: 3.5 seconds.
    1000000th fib # is: 1953282128707757731632 .... 93411568996526838242546875
    Time to compute: 58.1 seconds.
    */
    public class Fib {
        public static void main(String... args) {
            int place = args.length > 0 ? Integer.parseInt(args[0]) : 1000 * 1000;
            long start = System.nanoTime();
            BigInteger fibNumber = fib(place);
            long time = System.nanoTime() - start;
    
            System.out.println(place + "th fib # is: " + fibNumber);
            System.out.printf("Time to compute: %5.1f seconds.%n", time / 1.0e9);
        }
    
        private static BigInteger fib(int place) {
            BigInteger a = new BigInteger("0");
            BigInteger b = new BigInteger("1");
            while (place-- > 1) {
                BigInteger t = b;
                b = a.add(b);
                a = t;
            }
            return b;
        }
    }
    
    0 讨论(0)
  • 2020-12-17 03:27

    It looks better with multiple statements of ternary operator.

    static int fib(int n) {
            return  n > 5 ? fib(n-2) + fib(n-1)
                          : n < 2 || n == 5 ? n
                                            : n - 1;
    }
    
    0 讨论(0)
  • 2020-12-17 03:30

    The problem is that because you are using simple recursion, you re-evaluate F(n) multiple times, so your execution time is exponential.

    There are two simple ways to fix this:

    1) Cache values of F(n) when they are evaluated the first time. Check the cache first before evaluating F(n) to see if you have already calculated it for this n.

    2) Use an iterative approach: Calculate F(1), F(2), F(3), etc... until you reach the number you need.

    0 讨论(0)
  • 2020-12-17 03:31

    Naive implementation is natural and elegant but during execution recursive calls are creating binary tree. Beside already mentioned memoization, cashing of previous F(n) results and avoiding of unnecessary tree traversal, you can go for tail call optimization, already mentioned iterative or matrix multiplication. For example, Java 8 memoization:

    private static final Map<Long, Long> memo = new HashMap<>();
    static {
      memo.put(0L, 0L);
      memo.put(1L, 1L);
    }
    public static void main(String[] args) {
      System.out.println(fibonacci(0));
      System.out.println(fibonacci(43));
      System.out.println(fibonacci(92));
    }
    public static long fibonacci(long n) {
      return memo.computeIfAbsent(n, m -> fibonacci(m - 1) + fibonacci(m - 2));
    }
    

    Or maybe tail call optimized version:

    interface FewArgs<T, U, V, R> {
      public R apply(T t, U u, V v);
    }
    
    static FewArgs<Long, Long, Long, Long> tailRecursive;
    
    static {
      tailRecursive = (a, b, n) -> {
        if (n > 0)
          return tailRecursive.apply(b, a + b, n - 1);
        return a;
      };
    }
    

    You call it with a = 0, b = 1, n is required nth Fibonacci number but must be smaller than 93. More efficient ways to calculate Fibonacci numbers are matrix squaring, you will find example on my blog, and Binet formula

    0 讨论(0)
  • 2020-12-17 03:33
    import java.util.*;
    public class FibonacciNumber
    {
    
      public static void main(String[] args)
      {
        int high = 1, low = 1;
        int num;
        Scanner in = new Scanner(System.in);
        try
        {
          System.out.print("Enter Number : " );
          num = in.nextInt(); 
          System.out.println( low);
          while(high < num && num < 2000000000)
          {
            System.out.println(high);
            high = low + high;
            low = high - low;
          }
         } catch (InputMismatchException e) {
           System.out.print("Limit Exceeded");
         }
       }
    }
    
    /* Ouput : 
    Enter Number : 1999999999
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144
    233
    377
    610
    987
    1597
    2584
    4181
    6765
    10946
    17711
    28657
    46368
    75025
    121393
    196418
    317811
    514229
    832040
    1346269
    2178309
    3524578
    5702887
    9227465
    14930352
    24157817
    39088169
    63245986
    102334155
    165580141
    267914296
    433494437
    701408733
    1134903170
    1836311903
    -1323752223
    512559680
    -811192543
    -298632863
    -1109825406
    -1408458269
     1776683621
     368225352   */
    
    0 讨论(0)
提交回复
热议问题