KlimentHristov

**Tribonacci

Nov 11th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7. class Tribonacci
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             BigInteger firstN = BigInteger.Parse(Console.ReadLine());
  12.             BigInteger secondN = BigInteger.Parse(Console.ReadLine());
  13.             BigInteger thirdN = BigInteger.Parse(Console.ReadLine());
  14.             BigInteger n = BigInteger.Parse(Console.ReadLine());
  15.            
  16.             if(n == 1)
  17.             {
  18.                 Console.WriteLine(firstN);
  19.             }
  20.             else if(n == 2)
  21.             {
  22.                 Console.WriteLine(secondN);
  23.             }
  24.             else if(n == 3)
  25.             {
  26.                 Console.WriteLine(thirdN);
  27.             }
  28.                
  29.             else
  30.             {
  31.                 BigInteger result = 0;
  32.                     for (int i = 3; i < n; i++)
  33.                 {
  34.                     result = firstN +secondN+thirdN;
  35.                     firstN = secondN;
  36.                     secondN = thirdN;
  37.                     thirdN = result;
  38.                 }
  39.                 Console.WriteLine(result);
  40.             }
  41.            
  42.  
  43.         }
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment