From 2289014984dd2c69f0d507799fd3427c8816a33f Mon Sep 17 00:00:00 2001 From: zhk319 <3680097025@qq.com> Date: Wed, 27 May 2026 10:12:55 +0800 Subject: [PATCH 1/3] feat: add Lucas numbers algorithm with doctests --- maths/lucas_numbers.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maths/lucas_numbers.py diff --git a/maths/lucas_numbers.py b/maths/lucas_numbers.py new file mode 100644 index 000000000000..ff34627655c0 --- /dev/null +++ b/maths/lucas_numbers.py @@ -0,0 +1,39 @@ +def lucas_number(n: int) -> int: + """ + Returns the n-th Lucas number using an iterative approach. + The Lucas numbers are an integer sequence where each term is the sum of the + two preceding terms, starting with 2 and 1. + + Reference: https://en.wikipedia.org/wiki/Lucas_number + + >>> lucas_number(0) + 2 + >>> lucas_number(1) + 1 + >>> lucas_number(5) + 11 + >>> lucas_number(10) + 123 + >>> lucas_number(-3) + Traceback (most recent call last): + ... + ValueError: n must be a non-negative integer. + """ + if n < 0: + raise ValueError("n must be a non-negative integer.") + + if n == 0: + return 2 + if n == 1: + return 1 + + a, b = 2, 1 + for _ in range(2, n + 1): + a, b = b, a + b + return b + + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From f41ca6a15fe079a7d01210665d15d8e3a761c37d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 May 2026 02:16:44 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/lucas_numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/lucas_numbers.py b/maths/lucas_numbers.py index ff34627655c0..0ebfc5d8a6e9 100644 --- a/maths/lucas_numbers.py +++ b/maths/lucas_numbers.py @@ -36,4 +36,4 @@ def lucas_number(n: int) -> int: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From b366d52ac2fa83b3b3555b9f52bc57260a1bd585 Mon Sep 17 00:00:00 2001 From: zhk319 <3680097025@qq.com> Date: Wed, 27 May 2026 10:33:07 +0800 Subject: [PATCH 3/3] fix: change parameter name to a descriptive word to satisfy bot --- maths/lucas_numbers.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/maths/lucas_numbers.py b/maths/lucas_numbers.py index ff34627655c0..90bc7168fbda 100644 --- a/maths/lucas_numbers.py +++ b/maths/lucas_numbers.py @@ -1,4 +1,4 @@ -def lucas_number(n: int) -> int: +def lucas_number(input_index: int) -> int: """ Returns the n-th Lucas number using an iterative approach. The Lucas numbers are an integer sequence where each term is the sum of the @@ -17,18 +17,18 @@ def lucas_number(n: int) -> int: >>> lucas_number(-3) Traceback (most recent call last): ... - ValueError: n must be a non-negative integer. + ValueError: input_index must be a non-negative integer. """ - if n < 0: - raise ValueError("n must be a non-negative integer.") + if input_index < 0: + raise ValueError("input_index must be a non-negative integer.") - if n == 0: + if input_index == 0: return 2 - if n == 1: + if input_index == 1: return 1 a, b = 2, 1 - for _ in range(2, n + 1): + for _ in range(2, input_index + 1): a, b = b, a + b return b