Advertisement
Hristo_B

Tribonacci

May 31st, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class Tribonacci
  5. {
  6.     static void Main()
  7.     {
  8.         BigInteger firstNum = BigInteger.Parse(Console.ReadLine());
  9.         BigInteger secondNum = BigInteger.Parse(Console.ReadLine());
  10.         BigInteger thirdNum = BigInteger.Parse(Console.ReadLine());
  11.         int n = int.Parse(Console.ReadLine());
  12.  
  13.         int deviser = n % 3;
  14.         if (deviser == 1)
  15.         {
  16.             n += 2;
  17.         }
  18.         else if (deviser == 2)
  19.         {
  20.             n++;
  21.         }
  22.         int numberOfCycles = (n / 3) - 1;
  23.         for (int i = 0; i < numberOfCycles; i++)
  24.         {
  25.             firstNum = firstNum + secondNum + thirdNum;
  26.             secondNum = secondNum + firstNum + thirdNum;
  27.             thirdNum = thirdNum + firstNum + secondNum;
  28.         }
  29.         if (deviser == 0)
  30.         {
  31.             Console.WriteLine(thirdNum);
  32.         }
  33.         else if (deviser == 1)
  34.         {
  35.             Console.WriteLine(firstNum);
  36.         }
  37.         else
  38.         {
  39.             Console.WriteLine(secondNum);
  40.         }
  41.        
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement