Guest User

Untitled

a guest
Apr 22nd, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. /* File: NumberOfSubsetsWithTargetSum.java
  2. * Created: 2017-04-13
  3. * Author: Sabbir Manandhar
  4. *
  5. * Copyright (c) 2017 manandharsabbirk.appspot.com
  6. */
  7.  
  8. /**
  9. * Computes Number of Sub-sets that have sums to targetSUm
  10. *
  11. * @author Sabbir Manandhar manandhar.sabbir@gmail.com
  12. * @version 1.0
  13. */
  14. public class Fibonacci {
  15.  
  16. /**
  17. * main method
  18. */
  19. public static void main(String[] args) {
  20. int n = 50;
  21. long now = System.currentTimeMillis();
  22. System.out.println(fib_memoization(n, new long[n+1]));
  23. System.out.println("Computed with Memoization in: " + (System.currentTimeMillis()-now));
  24. now = System.currentTimeMillis();
  25. //System.out.println(fib_recursion(n));
  26. System.out.println("Computed with Recursion in: " + (System.currentTimeMillis()-now));
  27. now = System.currentTimeMillis();
  28. System.out.println(fib_dp(n));
  29. System.out.println("Computed with DP in: " + (System.currentTimeMillis()-now));
  30. now = System.currentTimeMillis();
  31. System.out.println(fib_dp_modified(n));
  32. System.out.println("Computed with modified DP in: " + (System.currentTimeMillis()-now));
  33. } // main
  34.  
  35. //--------------------------------------------------------------------------
  36.  
  37. /**
  38. * Compute nth Fibonacci number using Recursion
  39. */
  40. public static long fib_recursion(int n) {
  41. if (n == 0) {
  42. return 0;
  43. }
  44.  
  45. if (n == 1) {
  46. return 1;
  47. }
  48.  
  49. return fib_recursion(n-2) + fib_recursion(n-1);
  50. } // fib
  51.  
  52. //--------------------------------------------------------------------------
  53.  
  54. /**
  55. * Compute nth Fibonacci number using memoization
  56. */
  57. public static long fib_memoization(int n, long[] mem) {
  58. if (n == 0 || n == 1) {
  59. mem[n] = n;
  60. return n;
  61. }
  62.  
  63. if (mem[n] == 0) {
  64. mem[n] = fib_memoization(n-2, mem) + fib_memoization(n-1, mem);
  65. }
  66. return mem[n];
  67. } // fib
  68.  
  69. //--------------------------------------------------------------------------
  70.  
  71. /**
  72. * Compute nth Fibonacci number using DP
  73. */
  74. public static long fib_dp(int n) {
  75. long[] dp = new long[n+1];
  76.  
  77. for (int i = 0; i <= n; i++) {
  78. if (i == 0 || i == 1) {
  79. dp[i] = i;
  80. continue;
  81. }
  82. dp[i] = dp[i-1] + dp[i-2];
  83. }
  84. return dp[n];
  85. } // fib3
  86.  
  87. //--------------------------------------------------------------------------
  88.  
  89. /**
  90. * Compute nth Fibonacci number using modified DP
  91. */
  92. public static long fib_dp_modified(int n) {
  93. if (n == 0 || n == 1) {
  94. return n;
  95. }
  96.  
  97. long first = 0;
  98. long second = 1;
  99.  
  100. int k = 2;
  101. do {
  102. long temp = first + second;
  103. first = second;
  104. second = temp;
  105. if (k == n) {
  106. return second;
  107. }
  108. k++;
  109. } while (true);
  110. } // fib4
  111.  
  112. //--------------------------------------------------------------------------
  113. } // Fibonacci
Add Comment
Please, Sign In to add comment