Venciity

[SoftUni C# exam preparation] 02.Tribonacci

Apr 9th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4.     class Tribonacci
  5.     {
  6.         static void Main()
  7.         {
  8.             BigInteger trib1 = new BigInteger(Int32.Parse(Console.ReadLine()));
  9.             BigInteger trib2 = new BigInteger(Int32.Parse(Console.ReadLine()));
  10.             BigInteger trib3 = new BigInteger(Int32.Parse(Console.ReadLine()));
  11.             int n = Int32.Parse(Console.ReadLine());
  12.  
  13.             if (n == 1)
  14.             {
  15.                 Console.WriteLine(trib1);
  16.             }
  17.             else if (n == 2)
  18.             {
  19.                 Console.WriteLine(trib2);
  20.             }
  21.             else if (n == 3)
  22.             {
  23.                 Console.WriteLine(trib3);
  24.             }
  25.             else
  26.             {
  27.                 for (int i = 4; i <= n; i++)
  28.                 {
  29.                     BigInteger tribNew = trib1 + trib2 + trib3;
  30.                     trib1 = trib2;
  31.                     trib2 = trib3;
  32.                     trib3 = tribNew;
  33.                 }
  34.                 Console.WriteLine(trib3);
  35.             }
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment