You all know the Fibonacci sequence. Well, the Tribonacci sequence is almost the same, but it uses the last three numbers (instead of the last two) to calculate the next number in the sequence. So, we can define each element in the sequence as:

T n = T n-1 + T n-2 + T n-3

where T n is the current Tribonacci number (n is the index of the current Tribonacci number).

The Tribonacci sequence can begin with any three integer numbers – positive or negative – and continue as described by the formula above.

Now, a Tribonacci triangle is a triangle of numbers from the Tribonacci sequence. The first line of the triangle contains only the first number of the Tribonacci sequence. The second line contains the second and third numbers of the Tribonacci sequence, separated by a single whitespace (” “). The third line contains the next three numbers of the Tribonacci sequence (again, separated by whitespaces). The fourth line contains the next four numbers and so on. Basically, every line contains one more number than the previous.

Your task is to write a program, which prints to the console a Tribonacci triangle by given the first three numbers of the Tribonacci sequence, and the number of lines in the triangle.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; class Tribonacci { static void Main(string[] args) { long first = long.Parse(Console.ReadLine()); long second = long.Parse(Console.ReadLine()); long third = long.Parse(Console.ReadLine()); int L = int.Parse(Console.ReadLine()); long fourt = first + second + third; if (L == 2) { Console.WriteLine(first); Console.WriteLine(second + " " + third); } else { Console.WriteLine(first); Console.WriteLine(second + " " + third); Console.Write(fourt + " "); for (int k = 0; k < 2; k++) { first = second; second = third; third = fourt; fourt = first + second + third; Console.Write("{0} ", fourt); } Console.WriteLine(); for (int i = 0; i < L - 3; i++) { for (int j = 0; j < i + 4; j++) { first = second; second = third; third = fourt; fourt = first + second + third; Console.Write("{0} ", fourt); } Console.WriteLine(); } } } } |