From 76529e11f04d24d349f11b0963848899dddf4dbd Mon Sep 17 00:00:00 2001 From: megha-66 Date: Mon, 13 Oct 2025 09:07:59 +0000 Subject: [PATCH 1/3] Algorithm for tribonacci number calculation added --- maths/tribonacci.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 maths/tribonacci.py diff --git a/maths/tribonacci.py b/maths/tribonacci.py new file mode 100644 index 000000000000..f7a785a28b90 --- /dev/null +++ b/maths/tribonacci.py @@ -0,0 +1,21 @@ +""" +This program calculates the "Nth Tribonacci number in a series. + +The Tribonacci sequence Tn is defined as follows :- + +T(0) = 0; T(1) = 1; T(2) = 1; and T(n+3) = T(n) + T(n+1) + T(n+3) for n>=0 + +In this program, we assume an integer 'n' is given and we have to calculate nth Tribinacci number + +""" + +def tribonacci(self, n : int) -> int : + trib = [0,1,1] + for i in range(3,n+1): + x = trib[i-1] + trib[i-2] + trib[i-3] + trib.append(x) + return trib[n] + + +if __name__ == "__main__" : + print(tribonacci(25)) #prints 1389537 From 0fbe0dbbd09044e9411504c2634cd73ba2daf988 Mon Sep 17 00:00:00 2001 From: megha-66 Date: Mon, 13 Oct 2025 09:56:52 +0000 Subject: [PATCH 2/3] Added a new algorithm --- maths/tribonacci.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/maths/tribonacci.py b/maths/tribonacci.py index f7a785a28b90..17a3882df7cb 100644 --- a/maths/tribonacci.py +++ b/maths/tribonacci.py @@ -1,15 +1,16 @@ """ -This program calculates the "Nth Tribonacci number in a series. +This program calculates the "Nth Tribonacci number in a series. -The Tribonacci sequence Tn is defined as follows :- +The Tribonacci sequence Tn is defined as follows :- -T(0) = 0; T(1) = 1; T(2) = 1; and T(n+3) = T(n) + T(n+1) + T(n+3) for n>=0 +T(0) = 0; T(1) = 1; T(2) = 1; and T(n+3) = T(n) + T(n+1) + T(n+3) for n>=0 -In this program, we assume an integer 'n' is given and we have to calculate nth Tribinacci number +In this program, we assume an integer 'n' is given and +we have to calculate nth Tribinacci number """ -def tribonacci(self, n : int) -> int : +def tribonacci(n : int) -> int : trib = [0,1,1] for i in range(3,n+1): x = trib[i-1] + trib[i-2] + trib[i-3] @@ -18,4 +19,5 @@ def tribonacci(self, n : int) -> int : if __name__ == "__main__" : - print(tribonacci(25)) #prints 1389537 + import doctest + doctest.testmod() From 1d0c4dacea63b43fd4b8eca9e907092ad20aafea Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Oct 2025 10:03:53 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/tribonacci.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/maths/tribonacci.py b/maths/tribonacci.py index 17a3882df7cb..35933cccc938 100644 --- a/maths/tribonacci.py +++ b/maths/tribonacci.py @@ -10,14 +10,16 @@ """ -def tribonacci(n : int) -> int : - trib = [0,1,1] - for i in range(3,n+1): - x = trib[i-1] + trib[i-2] + trib[i-3] + +def tribonacci(n: int) -> int: + trib = [0, 1, 1] + for i in range(3, n + 1): + x = trib[i - 1] + trib[i - 2] + trib[i - 3] trib.append(x) return trib[n] -if __name__ == "__main__" : +if __name__ == "__main__": import doctest + doctest.testmod()