fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.math.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. for(long i = 0; i < 35; i++) {
  10. System.out.println(String.format("%9d: %d", i, fib(i)));
  11. }
  12. }
  13.  
  14. private static long fib(long n) {
  15. if(n == 0 || n == 1) {
  16. return 1;
  17. }
  18.  
  19. long n0 = 1;
  20. long n1 = 1;
  21. long curr = n0 + n1;
  22. for(long i = 1; i < n; i++) {
  23. curr = n0 + n1;
  24. n0 = n1;
  25. n1 = curr;
  26. }
  27.  
  28. return curr;
  29. }
  30. }
Success #stdin #stdout 0.1s 212736KB
stdin
Standard input is empty
stdout
        0: 1
        1: 1
        2: 2
        3: 3
        4: 5
        5: 8
        6: 13
        7: 21
        8: 34
        9: 55
       10: 89
       11: 144
       12: 233
       13: 377
       14: 610
       15: 987
       16: 1597
       17: 2584
       18: 4181
       19: 6765
       20: 10946
       21: 17711
       22: 28657
       23: 46368
       24: 75025
       25: 121393
       26: 196418
       27: 317811
       28: 514229
       29: 832040
       30: 1346269
       31: 2178309
       32: 3524578
       33: 5702887
       34: 9227465