Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. [TestClass]
  2. public class FibonacciSequenceTest
  3. {
  4. private const long Number = 40;
  5. private const long Result = 102334155;
  6.  
  7. private readonly FibonacciSequence fibonacciSequence;
  8.  
  9. public FibonacciSequenceTest()
  10. {
  11. fibonacciSequence = new FibonacciSequence();
  12. }
  13.  
  14. [TestMethod]
  15. public void MatrixFibonacciCalculatorTest()
  16. {
  17. // Act
  18. var returnValue = fibonacciSequence.MatrixFibonacciCalculator(Number);
  19.  
  20. // Assert
  21. long actual = returnValue;
  22. Assert.AreEqual(actual, Result);
  23. }
  24. }
  25.  
  26. public class FibonacciSequence
  27. {
  28. private readonly long max = 1000;
  29.  
  30. private readonly long[] memoizedFibonacciNumbers;
  31.  
  32. public FibonacciSequence()
  33. {
  34. memoizedFibonacciNumbers = new[] { max };
  35. }
  36.  
  37. #region MatrixFibonnaciCalculator
  38.  
  39. public long MatrixFibonacciCalculator(long n)
  40. {
  41. long[,] f = { { 1, 1 }, { 1, 0 } };
  42. if (n == 0)
  43. return 0;
  44. PowerMatrix1(f, n - 1);
  45.  
  46. return f[0, 0];
  47. }
  48.  
  49. /* Helper function that multiplies 2
  50. matrices F and M of size 2*2, and puts
  51. the multiplication result back to F[][] */
  52. public void MultiplyMatrix1(long[,] F, long[,] M)
  53. {
  54. long x = F[0, 0] * M[0, 0] + F[0, 1] * M[1, 0];
  55. long y = F[0, 0] * M[0, 1] + F[0, 1] * M[1, 1];
  56. long z = F[1, 0] * M[0, 0] + F[1, 1] * M[1, 0];
  57. long w = F[1, 0] * M[0, 1] + F[1, 1] * M[1, 1];
  58.  
  59. F[0, 0] = x;
  60. F[0, 1] = y;
  61. F[1, 0] = z;
  62. F[1, 1] = w;
  63. }
  64.  
  65. /* Helper function that calculates F[][]
  66. raise to the power n and puts the result
  67. in F[][] Note that this function is designed
  68. only for fib() and won't work as general
  69. power function */
  70. public void PowerMatrix1(long[,] F, long n)
  71. {
  72. long i;
  73. var M = new long[,] { { 1, 1 }, { 1, 0 } };
  74.  
  75. // n - 1 times multiply the matrix to
  76. // {{1,0},{0,1}}
  77. for (i = 2; i <= n; i++)
  78. MultiplyMatrix1(F, M);
  79. }
  80.  
  81. #endregion
  82.  
  83. #region EfficentMatrixFibonacciCalculator
  84.  
  85. public long EfficientMatrixFibonacciCalculator(long n)
  86. {
  87. var f = new long[,] { { 1, 1 }, { 1, 0 } };
  88. if (n == 0)
  89. return 0;
  90. EfficientPowerMatrix(f, n - 1);
  91.  
  92. return f[0, 0];
  93. }
  94.  
  95. public void EfficientPowerMatrix(long[,] F, long n)
  96. {
  97. if (n == 0 || n == 1)
  98. return;
  99. var M = new long[,] { { 1, 1 }, { 1, 0 } };
  100.  
  101. EfficientPowerMatrix(F, n / 2);
  102. EfficientMultiplyMatrix(F, F);
  103.  
  104. if (n % 2 != 0)
  105. EfficientMultiplyMatrix(F, M);
  106. }
  107.  
  108. public void EfficientMultiplyMatrix(long[,] f, long[,] m)
  109. {
  110. long x = f[0, 0] * m[0, 0] + f[0, 1] * m[1, 0];
  111. long y = f[0, 0] * m[0, 1] + f[0, 1] * m[1, 1];
  112. long z = f[1, 0] * m[0, 0] + f[1, 1] * m[1, 0];
  113. long w = f[1, 0] * m[0, 1] + f[1, 1] * m[1, 1];
  114.  
  115. f[0, 0] = x;
  116. f[0, 1] = y;
  117. f[1, 0] = z;
  118. f[1, 1] = w;
  119. }
  120.  
  121. #endregion
  122.  
  123.  
  124.  
  125. public int IterativeFibonacciCalculator(long number)
  126. {
  127. int firstNumber = 0, secondNumber = 1, result = 0;
  128.  
  129. if (number == 0) return 0; // To return the first Fibonacci number
  130. if (number == 1) return 1; // To return the second Fibonacci number
  131.  
  132. for (var i = 2; i <= number; i++)
  133. {
  134. result = firstNumber + secondNumber;
  135. firstNumber = secondNumber;
  136. secondNumber = result;
  137. }
  138.  
  139. return result;
  140. }
  141.  
  142. public long RecursiveFibonacciCalculator(long number)
  143. {
  144. if (number <= 1)
  145. {
  146. return number;
  147. }
  148.  
  149. return RecursiveFibonacciCalculator(number - 1) + RecursiveFibonacciCalculator(number - 2);
  150. }
  151.  
  152. public long DynamicFibonacciCalculator(long number)
  153. {
  154. long result;
  155. var memoArrays = new long[number + 1];
  156. if (memoArrays[number] != 0) return memoArrays[number];
  157. if (number == 1 || number == 2)
  158. {
  159. result = 1;
  160. }
  161. else
  162. {
  163. result = DynamicFibonacciCalculator(number - 1) + DynamicFibonacciCalculator(number - 2);
  164. memoArrays[number] = result;
  165. }
  166.  
  167. return result;
  168. }
  169.  
  170. public long DynamicFibonacciCalculator2(long n)
  171. {
  172. // Declare an array to
  173. // store Fibonacci numbers.
  174. // 1 extra to handle
  175. // case, n = 0
  176. var f = new long[n + 2];
  177. long i;
  178.  
  179. /* 0th and 1st number of the
  180. series are 0 and 1 */
  181. f[0] = 0;
  182. f[1] = 1;
  183.  
  184. for (i = 2; i <= n; i++)
  185. /* Add the previous 2 numbers
  186. in the series and store it */
  187. f[i] = f[i - 1] + f[i - 2];
  188.  
  189. return f[n];
  190. }
  191.  
  192. // Helper method for PiCalculator
  193. public long PiFibonacciCalculator(long n)
  194. {
  195. double phi = (1 + Math.Sqrt(5)) / 2;
  196. return (long)Math.Round(Math.Pow(phi, n) / Math.Sqrt(5));
  197. }
  198.  
  199.  
  200.  
  201. public long BottomUpFibonacciCalculator(long n)
  202. {
  203. long a = 0, b = 1;
  204.  
  205. // To return the first Fibonacci number
  206. if (n == 0) return a;
  207.  
  208. for (long i = 2; i <= n; i++)
  209. {
  210. long c = a + b;
  211. a = b;
  212. b = c;
  213. }
  214.  
  215. return b;
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement