Advertisement
tockata

Tribonacci

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