mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
[mypy] fix type annotations for all Project Euler problems (#4747)
* [mypy] fix type annotations for problem003/sol1 and problem003/sol3 * [mypy] fix type annotations for project euler problem007/sol2 * [mypy] fix type annotations for project euler problem008/sol2 * [mypy] fix type annotations for project euler problem009/sol1 * [mypy] fix type annotations for project euler problem014/sol1 * [mypy] fix type annotations for project euler problem 025/sol2 * [mypy] fix type annotations for project euler problem026/sol1.py * [mypy] fix type annotations for project euler problem037/sol1 * [mypy] fix type annotations for project euler problem044/sol1 * [mypy] fix type annotations for project euler problem046/sol1 * [mypy] fix type annotations for project euler problem051/sol1 * [mypy] fix type annotations for project euler problem074/sol2 * [mypy] fix type annotations for project euler problem080/sol1 * [mypy] fix type annotations for project euler problem099/sol1 * [mypy] fix type annotations for project euler problem101/sol1 * [mypy] fix type annotations for project euler problem188/sol1 * [mypy] fix type annotations for project euler problem191/sol1 * [mypy] fix type annotations for project euler problem207/sol1 * [mypy] fix type annotations for project euler problem551/sol1
This commit is contained in:
parent
e311b02e70
commit
bcfca67faa
|
@ -92,8 +92,8 @@ def solution(n: int = 600851475143) -> int:
|
||||||
return n
|
return n
|
||||||
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
||||||
if n % i == 0:
|
if n % i == 0:
|
||||||
if isprime(n / i):
|
if isprime(n // i):
|
||||||
max_number = n / i
|
max_number = n // i
|
||||||
break
|
break
|
||||||
elif isprime(i):
|
elif isprime(i):
|
||||||
max_number = i
|
max_number = i
|
||||||
|
|
|
@ -57,7 +57,7 @@ def solution(n: int = 600851475143) -> int:
|
||||||
i += 1
|
i += 1
|
||||||
ans = i
|
ans = i
|
||||||
while n % i == 0:
|
while n % i == 0:
|
||||||
n = n / i
|
n = n // i
|
||||||
i += 1
|
i += 1
|
||||||
return int(ans)
|
return int(ans)
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,7 @@ def solution(nth: int = 10001) -> int:
|
||||||
raise TypeError("Parameter nth must be int or castable to int.") from None
|
raise TypeError("Parameter nth must be int or castable to int.") from None
|
||||||
if nth <= 0:
|
if nth <= 0:
|
||||||
raise ValueError("Parameter nth must be greater than or equal to one.")
|
raise ValueError("Parameter nth must be greater than or equal to one.")
|
||||||
primes = []
|
primes: list[int] = []
|
||||||
num = 2
|
num = 2
|
||||||
while len(primes) < nth:
|
while len(primes) < nth:
|
||||||
if isprime(num):
|
if isprime(num):
|
||||||
|
|
|
@ -70,7 +70,9 @@ def solution(n: str = N) -> int:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return max(
|
return max(
|
||||||
reduce(lambda x, y: int(x) * int(y), n[i : i + 13]) for i in range(len(n) - 12)
|
# mypy cannot properly interpret reduce
|
||||||
|
int(reduce(lambda x, y: str(int(x) * int(y)), n[i : i + 13]))
|
||||||
|
for i in range(len(n) - 12)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,8 @@ def solution() -> int:
|
||||||
if (a ** 2) + (b ** 2) == (c ** 2):
|
if (a ** 2) + (b ** 2) == (c ** 2):
|
||||||
return a * b * c
|
return a * b * c
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
def solution_fast() -> int:
|
def solution_fast() -> int:
|
||||||
"""
|
"""
|
||||||
|
@ -55,6 +57,8 @@ def solution_fast() -> int:
|
||||||
if a < b < c and (a ** 2) + (b ** 2) == (c ** 2):
|
if a < b < c and (a ** 2) + (b ** 2) == (c ** 2):
|
||||||
return a * b * c
|
return a * b * c
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
def benchmark() -> None:
|
def benchmark() -> None:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -44,7 +44,7 @@ def solution(n: int = 1000000) -> int:
|
||||||
|
|
||||||
while number > 1:
|
while number > 1:
|
||||||
if number % 2 == 0:
|
if number % 2 == 0:
|
||||||
number /= 2
|
number //= 2
|
||||||
counter += 1
|
counter += 1
|
||||||
else:
|
else:
|
||||||
number = (3 * number) + 1
|
number = (3 * number) + 1
|
||||||
|
|
|
@ -23,9 +23,10 @@ The 12th term, F12, is the first term to contain three digits.
|
||||||
What is the index of the first term in the Fibonacci sequence to contain 1000
|
What is the index of the first term in the Fibonacci sequence to contain 1000
|
||||||
digits?
|
digits?
|
||||||
"""
|
"""
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
|
|
||||||
def fibonacci_generator() -> int:
|
def fibonacci_generator() -> Generator[int, None, None]:
|
||||||
"""
|
"""
|
||||||
A generator that produces numbers in the Fibonacci sequence
|
A generator that produces numbers in the Fibonacci sequence
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ def solution(numerator: int = 1, digit: int = 1000) -> int:
|
||||||
longest_list_length = 0
|
longest_list_length = 0
|
||||||
|
|
||||||
for divide_by_number in range(numerator, digit + 1):
|
for divide_by_number in range(numerator, digit + 1):
|
||||||
has_been_divided = []
|
has_been_divided: list[int] = []
|
||||||
now_divide = numerator
|
now_divide = numerator
|
||||||
for division_cycle in range(1, digit + 1):
|
for division_cycle in range(1, digit + 1):
|
||||||
if now_divide in has_been_divided:
|
if now_divide in has_been_divided:
|
||||||
|
|
|
@ -76,7 +76,7 @@ def compute_truncated_primes(count: int = 11) -> list[int]:
|
||||||
>>> compute_truncated_primes(11)
|
>>> compute_truncated_primes(11)
|
||||||
[23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397]
|
[23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397]
|
||||||
"""
|
"""
|
||||||
list_truncated_primes = []
|
list_truncated_primes: list[int] = []
|
||||||
num = 13
|
num = 13
|
||||||
while len(list_truncated_primes) != count:
|
while len(list_truncated_primes) != count:
|
||||||
if validate(num):
|
if validate(num):
|
||||||
|
|
|
@ -42,6 +42,8 @@ def solution(limit: int = 5000) -> int:
|
||||||
if is_pentagonal(a) and is_pentagonal(b):
|
if is_pentagonal(a) and is_pentagonal(b):
|
||||||
return b
|
return b
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(f"{solution() = }")
|
print(f"{solution() = }")
|
||||||
|
|
|
@ -85,6 +85,8 @@ def compute_nums(n: int) -> list[int]:
|
||||||
if len(list_nums) == n:
|
if len(list_nums) == n:
|
||||||
return list_nums
|
return list_nums
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
def solution() -> int:
|
def solution() -> int:
|
||||||
"""Return the solution to the problem"""
|
"""Return the solution to the problem"""
|
||||||
|
|
|
@ -63,12 +63,12 @@ def digit_replacements(number: int) -> list[list[int]]:
|
||||||
>>> digit_replacements(3112)
|
>>> digit_replacements(3112)
|
||||||
[[3002, 3112, 3222, 3332, 3442, 3552, 3662, 3772, 3882, 3992]]
|
[[3002, 3112, 3222, 3332, 3442, 3552, 3662, 3772, 3882, 3992]]
|
||||||
"""
|
"""
|
||||||
number = str(number)
|
number_str = str(number)
|
||||||
replacements = []
|
replacements = []
|
||||||
digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
|
digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
|
||||||
|
|
||||||
for duplicate in Counter(number) - Counter(set(number)):
|
for duplicate in Counter(number_str) - Counter(set(number_str)):
|
||||||
family = [int(number.replace(duplicate, digit)) for digit in digits]
|
family = [int(number_str.replace(duplicate, digit)) for digit in digits]
|
||||||
replacements.append(family)
|
replacements.append(family)
|
||||||
|
|
||||||
return replacements
|
return replacements
|
||||||
|
@ -106,6 +106,8 @@ def solution(family_length: int = 8) -> int:
|
||||||
|
|
||||||
return min(primes_in_family)
|
return min(primes_in_family)
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(solution())
|
print(solution())
|
||||||
|
|
|
@ -20,8 +20,8 @@
|
||||||
counter increases.
|
counter increases.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
factorial_cache = {}
|
factorial_cache: dict[int, int] = {}
|
||||||
factorial_sum_cache = {}
|
factorial_sum_cache: dict[int, int] = {}
|
||||||
|
|
||||||
|
|
||||||
def factorial(a: int) -> int:
|
def factorial(a: int) -> int:
|
||||||
|
|
|
@ -26,8 +26,8 @@ def solution() -> int:
|
||||||
sqrt_number = number.sqrt(decimal_context)
|
sqrt_number = number.sqrt(decimal_context)
|
||||||
if len(str(sqrt_number)) > 1:
|
if len(str(sqrt_number)) > 1:
|
||||||
answer += int(str(sqrt_number)[0])
|
answer += int(str(sqrt_number)[0])
|
||||||
sqrt_number = str(sqrt_number)[2:101]
|
sqrt_number_str = str(sqrt_number)[2:101]
|
||||||
answer += sum(int(x) for x in sqrt_number)
|
answer += sum(int(x) for x in sqrt_number_str)
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,3 +35,4 @@ if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
print(f"{solution() = }")
|
||||||
|
|
|
@ -22,12 +22,14 @@ def solution(data_file: str = "base_exp.txt") -> int:
|
||||||
>>> solution()
|
>>> solution()
|
||||||
709
|
709
|
||||||
"""
|
"""
|
||||||
largest = [0, 0]
|
largest: float = 0
|
||||||
|
result = 0
|
||||||
for i, line in enumerate(open(os.path.join(os.path.dirname(__file__), data_file))):
|
for i, line in enumerate(open(os.path.join(os.path.dirname(__file__), data_file))):
|
||||||
a, x = list(map(int, line.split(",")))
|
a, x = list(map(int, line.split(",")))
|
||||||
if x * log10(a) > largest[0]:
|
if x * log10(a) > largest:
|
||||||
largest = [x * log10(a), i + 1]
|
largest = x * log10(a)
|
||||||
return largest[1]
|
result = i + 1
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -202,7 +202,7 @@ def solution(func: Callable[[int], int] = question_function, order: int = 10) ->
|
||||||
]
|
]
|
||||||
|
|
||||||
ret: int = 0
|
ret: int = 0
|
||||||
poly: int
|
poly: Callable[[int], int]
|
||||||
x_val: int
|
x_val: int
|
||||||
|
|
||||||
for poly in polynomials:
|
for poly in polynomials:
|
||||||
|
|
|
@ -19,7 +19,7 @@ References:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# small helper function for modular exponentiation
|
# small helper function for modular exponentiation (fast exponentiation algorithm)
|
||||||
def _modexpt(base: int, exponent: int, modulo_value: int) -> int:
|
def _modexpt(base: int, exponent: int, modulo_value: int) -> int:
|
||||||
"""
|
"""
|
||||||
Returns the modular exponentiation, that is the value
|
Returns the modular exponentiation, that is the value
|
||||||
|
@ -36,7 +36,7 @@ def _modexpt(base: int, exponent: int, modulo_value: int) -> int:
|
||||||
if exponent == 1:
|
if exponent == 1:
|
||||||
return base
|
return base
|
||||||
if exponent % 2 == 0:
|
if exponent % 2 == 0:
|
||||||
x = _modexpt(base, exponent / 2, modulo_value) % modulo_value
|
x = _modexpt(base, exponent // 2, modulo_value) % modulo_value
|
||||||
return (x * x) % modulo_value
|
return (x * x) % modulo_value
|
||||||
else:
|
else:
|
||||||
return (base * _modexpt(base, exponent - 1, modulo_value)) % modulo_value
|
return (base * _modexpt(base, exponent - 1, modulo_value)) % modulo_value
|
||||||
|
|
|
@ -26,7 +26,7 @@ References:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
cache = {}
|
cache: dict[tuple[int, int, int], int] = {}
|
||||||
|
|
||||||
|
|
||||||
def _calculate(days: int, absent: int, late: int) -> int:
|
def _calculate(days: int, absent: int, late: int) -> int:
|
||||||
|
|
|
@ -90,7 +90,7 @@ def solution(max_proportion: float = 1 / 12345) -> int:
|
||||||
perfect_partitions += 1
|
perfect_partitions += 1
|
||||||
if perfect_partitions > 0:
|
if perfect_partitions > 0:
|
||||||
if perfect_partitions / total_partitions < max_proportion:
|
if perfect_partitions / total_partitions < max_proportion:
|
||||||
return partition_candidate
|
return int(partition_candidate)
|
||||||
integer += 1
|
integer += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,10 @@ You are given a(10^6) = 31054319.
|
||||||
Find a(10^15)
|
Find a(10^15)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
ks = [k for k in range(2, 20 + 1)]
|
ks = [k for k in range(2, 20 + 1)]
|
||||||
base = [10 ** k for k in range(ks[-1] + 1)]
|
base = [10 ** k for k in range(ks[-1] + 1)]
|
||||||
memo = {}
|
memo: dict[int, dict[int, list[list[int]]]] = {}
|
||||||
|
|
||||||
|
|
||||||
def next_term(a_i, k, i, n):
|
def next_term(a_i, k, i, n):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user