-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_theory.py
More file actions
182 lines (135 loc) · 4.26 KB
/
Copy pathnumber_theory.py
File metadata and controls
182 lines (135 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""Pure number-theory helpers used by the Wolfram Beta toolkit."""
import math
from sympy import isprime, primefactors
from math import gcd
def check_prime(n: int) -> bool | None:
"""Check if a number is prime.
Args:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
try:
return isprime(n)
except (TypeError, ValueError):
print("Input must be an integer.")
return None
def is_prime(n: int) -> bool | None:
"""Alias for `check_prime` with a clearer name."""
return check_prime(n)
def prime_factorization(n: int) -> list | None:
"""Return the prime factorization of a number.
Args:
n (int): The number to factorize.
Returns:
list: The prime factors of the number.
"""
try:
return primefactors(n)
except (TypeError, ValueError):
print("Input must be an integer.")
return None
def prime_factors(n: int) -> list | None:
"""Alias for `prime_factorization` with a clearer name."""
return prime_factorization(n)
def power(base: float, exponent: float) -> float | None:
"""Return the power of a base number.
Args:
base (float): The base number.
exponent (float): The exponent.
Returns:
float: The result of the power operation.
"""
try:
return math.pow(base, exponent)
except (TypeError, ValueError):
print("Both base and exponent must be numbers.")
return None
def integer_power(base: float, exponent: float) -> float | None:
"""Alias for `power` with a clearer name."""
return power(base, exponent)
def sqrt(x: float) -> float | None:
"""Return the square root of a number.
Args:
x (float): The number to find the square root of.
Returns:
float: The square root of the number.
"""
try:
return math.sqrt(x)
except (TypeError, ValueError):
print("Input must be a non-negative number.")
return None
def find_gcd(x: int, y: int) -> int | None:
"""Find the greatest common divisor of two numbers.
Args:
x (int): The first number.
y (int): The second number.
Returns:
int: The greatest common divisor of the two numbers.
"""
try:
return gcd(x, y)
except (TypeError, ValueError):
print("Both x and y must be integers.")
return None
def gcd_pair(x: int, y: int) -> int | None:
"""Alias for `find_gcd` with a clearer name."""
return find_gcd(x, y)
def find_lcm(x: int, y: int) -> int | None:
"""Find the least common multiple of two numbers.
Args:
x (int): The first number.
y (int): The second number.
Returns:
int: The least common multiple of the two numbers.
"""
try:
return abs(x * y) // find_gcd(x, y)
except (TypeError, ValueError):
print("Both x and y must be integers.")
return None
def lcm_pair(x: int, y: int) -> int | None:
"""Alias for `find_lcm` with a clearer name."""
return find_lcm(x, y)
def fibonacci(n: int) -> int | None:
"""Return the nth number in the Fibonacci sequence.
Args:
n (int): The position in the Fibonacci sequence to return.
Returns:
int: The nth number in the Fibonacci sequence.
"""
try:
if n <= 0:
print("Input must be a positive integer.")
return None
elif n == 1:
return 1
else:
a, b = 0, 1
for _ in range(n - 1):
a, b = b, a + b
return b
except (TypeError, ValueError):
print("Input must be an integer.")
return None
def nth_fibonacci(n: int) -> int | None:
"""Alias for `fibonacci` with a clearer name."""
return fibonacci(n)
def factorial(n: int) -> int | None:
"""
Calculate the factorial of a non-negative integer.
Parameters:
n (int): The non-negative integer.
Returns:
int: The factorial of the input.
Raises:
ValueError: If the input is a negative integer.
"""
try:
if n < 0:
raise ValueError("Input must be a non-negative integer.")
return math.factorial(n)
except (TypeError, ValueError) as e:
print(f"Error: {e}")
return None