ellapt

Tribonacci

Dec 14th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class Tribonacci
  5. {
  6. static void Main()
  7. {
  8.  
  9. BigInteger tNminus1 = new BigInteger(int.Parse(Console.ReadLine()));
  10. BigInteger tNminus2 = new BigInteger(int.Parse(Console.ReadLine()));
  11. BigInteger tNminus3 = new BigInteger(int.Parse(Console.ReadLine()));
  12.  
  13. int numN = int.Parse(Console.ReadLine());
  14.  
  15. if (numN == 1)
  16. {
  17. Console.WriteLine(tNminus1);
  18. }
  19. else if (numN == 2)
  20. {
  21. Console.WriteLine(tNminus2);
  22. }
  23. else if (numN == 3)
  24. {
  25. Console.WriteLine(tNminus3);
  26. }
  27. else
  28. {
  29. for (int i = 4; i <= numN; i++)
  30. {
  31. BigInteger tribonacciNew = tNminus1 + tNminus2 + tNminus3;
  32. tNminus1 = tNminus2;
  33. tNminus2 = tNminus3;
  34. tNminus3 = tribonacciNew;
  35. }
  36. Console.WriteLine(tNminus3);
  37. }
  38.  
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment