Advertisement
sylviapsh

Tribonacci Nth Element

Dec 27th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. class TribonacciNthElement
  3. {
  4.   static void Main()
  5.   {
  6.     //Telerik Academy
  7.     //The Tribonacci sequence is a sequence in which every next element is made by the sum of the previous three elements from the sequence.
  8.     //Write a computer program that finds the Nth element of the Tribonacci sequence, if you are given the first three elements of the sequence and the number N. Mathematically said: with given T1, T2 and T3 – you must find Tn.
  9.  
  10.     int smallest = int.Parse(Console.ReadLine());
  11.     int middle = int.Parse(Console.ReadLine());
  12.     int biggest = int.Parse(Console.ReadLine());
  13.     int n = int.Parse(Console.ReadLine());
  14.  
  15.     for (int i = 4; i <= n; i++)
  16.     {
  17.       int next = smallest + middle + biggest;
  18.       smallest = middle;
  19.       middle = biggest;
  20.       biggest = next;
  21.     }
  22.     Console.WriteLine(biggest);
  23.   }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement