Skip to content

Commit 92af64a

Browse files
dbringcclauss
andauthored
Added function that calculates Pell numbers (#10343)
* Added function that calculates Pell numbers * Fixed ruff error * Renamed input argument to be more descriptive * updating DIRECTORY.md --------- Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 84e7972 commit 92af64a

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@
798798
* [Square Root](maths/numerical_analysis/square_root.py)
799799
* [Weierstrass Method](maths/numerical_analysis/weierstrass_method.py)
800800
* [Odd Sieve](maths/odd_sieve.py)
801+
* [Pell Number](maths/pell_number.py)
801802
* [Perfect Cube](maths/perfect_cube.py)
802803
* [Perfect Number](maths/perfect_number.py)
803804
* [Perfect Square](maths/perfect_square.py)

maths/pell_number.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
def pell_number_iterative(subscript: int) -> int:
2+
"""
3+
This function returns the `subscript`-th Pell number iteratively, where
4+
`subscript` is a non-negative integer. Pell numbers are defined by the
5+
recurrence relation:
6+
7+
P_0 = 0, P_1 = 1, P_n = 2 * P_(n-1) + P_(n-2)
8+
9+
https://en.wikipedia.org/wiki/Pell_number
10+
https://oeis.org/A000129
11+
12+
>>> pell_number_iterative(0)
13+
0
14+
>>> pell_number_iterative(1)
15+
1
16+
>>> pell_number_iterative(12)
17+
13860
18+
>>> pell_number_iterative("1")
19+
Traceback (most recent call last):
20+
...
21+
ValueError: The input must be an integer.
22+
>>> pell_number_iterative(-1)
23+
Traceback (most recent call last):
24+
...
25+
ValueError: The input number must be non-negative.
26+
"""
27+
if not isinstance(subscript, int):
28+
raise ValueError("The input must be an integer.")
29+
30+
if subscript < 0:
31+
raise ValueError("The input number must be non-negative.")
32+
33+
if subscript in (0, 1):
34+
return subscript
35+
36+
prev_prev_num = 0
37+
prev_num = 1
38+
39+
for _ in range(2, subscript + 1):
40+
temp = 2 * prev_num + prev_prev_num
41+
prev_prev_num = prev_num
42+
prev_num = temp
43+
44+
return prev_num
45+
46+
47+
def pell_number_recursive(subscript: int) -> int:
48+
"""
49+
This function calculates the `subscript`-th Pell number recursively. Due to
50+
its recursive nature, this function grows exponentially with `subscript`.
51+
For large values of `subscript`, use pell_number_iterative instead.
52+
53+
>>> pell_number_recursive(0)
54+
0
55+
>>> pell_number_recursive(1)
56+
1
57+
>>> pell_number_recursive(12)
58+
13860
59+
>>> pell_number_recursive("1")
60+
Traceback (most recent call last):
61+
...
62+
ValueError: The input must be an integer.
63+
>>> pell_number_recursive(-1)
64+
Traceback (most recent call last):
65+
...
66+
ValueError: The input number must be non-negative.
67+
"""
68+
if not isinstance(subscript, int):
69+
raise ValueError("The input must be an integer.")
70+
71+
if subscript < 0:
72+
raise ValueError("The input number must be non-negative.")
73+
74+
if subscript in (0, 1):
75+
return subscript
76+
77+
return 2 * pell_number_recursive(subscript - 1) + pell_number_recursive(
78+
subscript - 2
79+
)

0 commit comments

Comments
 (0)