From 912afb91af2dbd6dbb88cd49588d8af58ae1a0d4 Mon Sep 17 00:00:00 2001 From: AnaiMurillo Date: Wed, 1 Jan 2025 21:16:00 -0500 Subject: [PATCH] add solution for tribonacci sequence challenge --- solutions/the_tribonacci_sequence/main.py | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/solutions/the_tribonacci_sequence/main.py b/solutions/the_tribonacci_sequence/main.py index e69de29bb..44e2236a0 100644 --- a/solutions/the_tribonacci_sequence/main.py +++ b/solutions/the_tribonacci_sequence/main.py @@ -0,0 +1,24 @@ +def tribonacci(n: int) -> int: + """ + Calculate the nth tribonacci number. + + Args: + n (int): Index of the tribonacci number to calculate. + + Returns: + int: The nth tribonacci number. + + Created on 1/01/2025 + Author: Ana Isabel Murillo + """ + if n == 0: + return 0 + elif n == 1 or n == 2: + return 1 + + a, b, c = 0, 1, 1 + + for _ in range(3, n + 1): + a, b, c = b, c, a + b + c + + return c