fork download
  1. import java.lang.*;
  2.  
  3. class Fibonacci
  4. {
  5. static private double rootFive = Math.sqrt(5);
  6. static private double PHI = (1 + rootFive) / 2;
  7.  
  8. public static void main (String[] args)
  9. {
  10. long a = 0, b = 1, c;
  11. long num, FSum, sum = 0;
  12. // Greater values of n will over flow long.
  13. // Use higher data type for larger n.
  14. int n = 50;
  15.  
  16. for (int i = 0; i <= n; i++)
  17. {
  18. System.out.print(i + ":");
  19. if (i == 0 || i == 1) {
  20. c = i == 0 ? a : b;
  21. }
  22. else {
  23. c = a + b;
  24. a = b;
  25. b = c;
  26. }
  27. String tabs = "\t\t\t";
  28. System.out.print(c + tabs);
  29. num = fiboNum(i);
  30. System.out.print(fiboIndx(c) + ": " + num);
  31. System.out.print(tabs + "Diff: " + (num - c));
  32. sum += c;
  33. FSum = fiboSum(i);
  34. System.out.print(tabs + "Sum: " + sum + tabs + "Sum (Formula): " + FSum);
  35. System.out.println(tabs + "Diff: " + (FSum - sum));
  36. }
  37. }
  38. public static long fiboNum(int n)
  39. {
  40. return (long) Math.floor(Math.pow(PHI, n) / rootFive + 0.5);
  41. }
  42.  
  43. public static int fiboIndx(long num)
  44. {
  45. if (num == 0) return 0;
  46. return (int) Math.floor(Math.log(num * rootFive) / Math.log(PHI) + 0.5);
  47. }
  48.  
  49. public static long fiboSum(int n)
  50. {
  51. return fiboNum(n + 2) - 1;
  52. }
  53. }
Success #stdin #stdout 0.1s 380544KB
stdin
Standard input is empty
stdout
0:0			0: 0			Diff: 0			Sum: 0			Sum (Formula): 0			Diff: 0
1:1			2: 1			Diff: 0			Sum: 1			Sum (Formula): 1			Diff: 0
2:1			2: 1			Diff: 0			Sum: 2			Sum (Formula): 2			Diff: 0
3:2			3: 2			Diff: 0			Sum: 4			Sum (Formula): 4			Diff: 0
4:3			4: 3			Diff: 0			Sum: 7			Sum (Formula): 7			Diff: 0
5:5			5: 5			Diff: 0			Sum: 12			Sum (Formula): 12			Diff: 0
6:8			6: 8			Diff: 0			Sum: 20			Sum (Formula): 20			Diff: 0
7:13			7: 13			Diff: 0			Sum: 33			Sum (Formula): 33			Diff: 0
8:21			8: 21			Diff: 0			Sum: 54			Sum (Formula): 54			Diff: 0
9:34			9: 34			Diff: 0			Sum: 88			Sum (Formula): 88			Diff: 0
10:55			10: 55			Diff: 0			Sum: 143			Sum (Formula): 143			Diff: 0
11:89			11: 89			Diff: 0			Sum: 232			Sum (Formula): 232			Diff: 0
12:144			12: 144			Diff: 0			Sum: 376			Sum (Formula): 376			Diff: 0
13:233			13: 233			Diff: 0			Sum: 609			Sum (Formula): 609			Diff: 0
14:377			14: 377			Diff: 0			Sum: 986			Sum (Formula): 986			Diff: 0
15:610			15: 610			Diff: 0			Sum: 1596			Sum (Formula): 1596			Diff: 0
16:987			16: 987			Diff: 0			Sum: 2583			Sum (Formula): 2583			Diff: 0
17:1597			17: 1597			Diff: 0			Sum: 4180			Sum (Formula): 4180			Diff: 0
18:2584			18: 2584			Diff: 0			Sum: 6764			Sum (Formula): 6764			Diff: 0
19:4181			19: 4181			Diff: 0			Sum: 10945			Sum (Formula): 10945			Diff: 0
20:6765			20: 6765			Diff: 0			Sum: 17710			Sum (Formula): 17710			Diff: 0
21:10946			21: 10946			Diff: 0			Sum: 28656			Sum (Formula): 28656			Diff: 0
22:17711			22: 17711			Diff: 0			Sum: 46367			Sum (Formula): 46367			Diff: 0
23:28657			23: 28657			Diff: 0			Sum: 75024			Sum (Formula): 75024			Diff: 0
24:46368			24: 46368			Diff: 0			Sum: 121392			Sum (Formula): 121392			Diff: 0
25:75025			25: 75025			Diff: 0			Sum: 196417			Sum (Formula): 196417			Diff: 0
26:121393			26: 121393			Diff: 0			Sum: 317810			Sum (Formula): 317810			Diff: 0
27:196418			27: 196418			Diff: 0			Sum: 514228			Sum (Formula): 514228			Diff: 0
28:317811			28: 317811			Diff: 0			Sum: 832039			Sum (Formula): 832039			Diff: 0
29:514229			29: 514229			Diff: 0			Sum: 1346268			Sum (Formula): 1346268			Diff: 0
30:832040			30: 832040			Diff: 0			Sum: 2178308			Sum (Formula): 2178308			Diff: 0
31:1346269			31: 1346269			Diff: 0			Sum: 3524577			Sum (Formula): 3524577			Diff: 0
32:2178309			32: 2178309			Diff: 0			Sum: 5702886			Sum (Formula): 5702886			Diff: 0
33:3524578			33: 3524578			Diff: 0			Sum: 9227464			Sum (Formula): 9227464			Diff: 0
34:5702887			34: 5702887			Diff: 0			Sum: 14930351			Sum (Formula): 14930351			Diff: 0
35:9227465			35: 9227465			Diff: 0			Sum: 24157816			Sum (Formula): 24157816			Diff: 0
36:14930352			36: 14930352			Diff: 0			Sum: 39088168			Sum (Formula): 39088168			Diff: 0
37:24157817			37: 24157817			Diff: 0			Sum: 63245985			Sum (Formula): 63245985			Diff: 0
38:39088169			38: 39088169			Diff: 0			Sum: 102334154			Sum (Formula): 102334154			Diff: 0
39:63245986			39: 63245986			Diff: 0			Sum: 165580140			Sum (Formula): 165580140			Diff: 0
40:102334155			40: 102334155			Diff: 0			Sum: 267914295			Sum (Formula): 267914295			Diff: 0
41:165580141			41: 165580141			Diff: 0			Sum: 433494436			Sum (Formula): 433494436			Diff: 0
42:267914296			42: 267914296			Diff: 0			Sum: 701408732			Sum (Formula): 701408732			Diff: 0
43:433494437			43: 433494437			Diff: 0			Sum: 1134903169			Sum (Formula): 1134903169			Diff: 0
44:701408733			44: 701408733			Diff: 0			Sum: 1836311902			Sum (Formula): 1836311902			Diff: 0
45:1134903170			45: 1134903170			Diff: 0			Sum: 2971215072			Sum (Formula): 2971215072			Diff: 0
46:1836311903			46: 1836311903			Diff: 0			Sum: 4807526975			Sum (Formula): 4807526975			Diff: 0
47:2971215073			47: 2971215073			Diff: 0			Sum: 7778742048			Sum (Formula): 7778742048			Diff: 0
48:4807526976			48: 4807526976			Diff: 0			Sum: 12586269024			Sum (Formula): 12586269024			Diff: 0
49:7778742049			49: 7778742049			Diff: 0			Sum: 20365011073			Sum (Formula): 20365011073			Diff: 0
50:12586269025			50: 12586269025			Diff: 0			Sum: 32951280098			Sum (Formula): 32951280098			Diff: 0