From a0a7ca0267df72daab0c79ae6055b447eb1500ce Mon Sep 17 00:00:00 2001 From: Prakriti Manhar Date: Fri, 12 Dec 2025 13:37:30 +0530 Subject: [PATCH 1/2] Add Padovan Sequence algorithm in maths --- maths/padovan_sequence.py | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 maths/padovan_sequence.py diff --git a/maths/padovan_sequence.py b/maths/padovan_sequence.py new file mode 100644 index 000000000000..accbb341fbad --- /dev/null +++ b/maths/padovan_sequence.py @@ -0,0 +1,48 @@ +def padovan_sequence(n: int) -> int: + """ + Return the n-th term of the Padovan Sequence. + The Padovan sequence is the sequence of integers P(n) defined by the initial values + P(0) = P(1) = P(2) = 1 and the recurrence relation P(n) = P(n-2) + P(n-3). + + https://en.wikipedia.org/wiki/Padovan_sequence + + :param n: The index of the term to return. + :return: The n-th term of the Padovan Sequence. + + >>> padovan_sequence(0) + 1 + >>> padovan_sequence(1) + 1 + >>> padovan_sequence(2) + 1 + >>> padovan_sequence(3) + 2 + >>> padovan_sequence(4) + 2 + >>> padovan_sequence(5) + 3 + >>> padovan_sequence(10) + 12 + >>> padovan_sequence(-1) + Traceback (most recent call last): + ... + ValueError: Input must be a non-negative integer. + """ + if not isinstance(n, int) or n < 0: + raise ValueError("Input must be a non-negative integer.") + + if n <= 2: + return 1 + + p_prev_3, p_prev_2, p_prev_1 = 1, 1, 1 + + for _ in range(3, n + 1): + curr = p_prev_2 + p_prev_3 + p_prev_3, p_prev_2, p_prev_1 = p_prev_2, p_prev_1, curr + + return p_prev_1 + +if __name__ == "__main__": + import doctest + doctest.testmod() + print("Doctests passed!") From f022d1e84ae1a93042921ba919b0a8e67a984d04 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 12 Dec 2025 08:12:46 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/padovan_sequence.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/padovan_sequence.py b/maths/padovan_sequence.py index accbb341fbad..1ad756af2e28 100644 --- a/maths/padovan_sequence.py +++ b/maths/padovan_sequence.py @@ -42,7 +42,9 @@ def padovan_sequence(n: int) -> int: return p_prev_1 + if __name__ == "__main__": import doctest + doctest.testmod() print("Doctests passed!")