From 4cca12685df10c0ee14ae2c6a7ef1975e3ef5d18 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 7 Jan 2024 20:00:24 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=E2=80=9C1137.=20N-th=20Tribonacci?= =?UTF-8?q?=20Number=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/n-th-tribonacci-number.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 cs/n-th-tribonacci-number.cs diff --git a/cs/n-th-tribonacci-number.cs b/cs/n-th-tribonacci-number.cs new file mode 100644 index 0000000..98abbb6 --- /dev/null +++ b/cs/n-th-tribonacci-number.cs @@ -0,0 +1,18 @@ +public class Solution { + public int Tribonacci(int n) { + var sequence = new int[]{0, 1, 1}; + + if (n < 3) { + return sequence[n]; + } + + for (var i = 3; i <= n; ++i) { + var next = sequence.Sum(); + sequence[0] = sequence[1]; + sequence[1] = sequence[2]; + sequence[2] = next; + } + + return sequence[2]; + } +}