From c45234e58ffe189fe9c71b37cf4d13b40e498272 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 24 Apr 2024 10:11:36 +0200 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB1137.=20N-th=20Tribonacci=20N?= =?UTF-8?q?umber=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/n-th-tribonacci-number.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 rs/n-th-tribonacci-number.rs diff --git a/rs/n-th-tribonacci-number.rs b/rs/n-th-tribonacci-number.rs new file mode 100644 index 0000000..4526e8d --- /dev/null +++ b/rs/n-th-tribonacci-number.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn tribonacci(mut n: i32) -> i32 { + let mut seq = vec![0, 1, 1]; + + while n >= 3 { + let next = seq.iter().sum::(); + + seq[0] = seq[1]; + seq[1] = seq[2]; + seq[2] = next; + + n -= 1; + } + + seq[n as usize] + } +}