Add type hints in problems 21-30

This commit is contained in:
daniele 2024-09-29 15:57:30 +02:00
parent f8b5260349
commit 48ab60733d
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
10 changed files with 41 additions and 39 deletions

View File

@ -13,8 +13,8 @@ from projecteuler import sum_of_divisors, timing
@timing @timing
def p021(): def p021() -> None:
sum_ = 0 _sum = 0
for i in range(2, 10000): for i in range(2, 10000):
# Calculate the sum of proper divisors with the function # Calculate the sum of proper divisors with the function
@ -24,12 +24,12 @@ def p021():
# If i!=n and the sum of proper divisors of n=i, # If i!=n and the sum of proper divisors of n=i,
# sum the pair of numbers and add it to the total. # sum the pair of numbers and add it to the total.
if i != n and sum_of_divisors(n) == i: if i != n and sum_of_divisors(n) == i:
sum_ = sum_ + i + n _sum = _sum + i + n
sum_ = sum_ // 2 _sum = _sum // 2
print('Project Euler, Problem 21') print('Project Euler, Problem 21')
print(f'Answer: {sum_}') print(f'Answer: {_sum}')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -14,7 +14,7 @@ from projecteuler import timing
@timing @timing
def p022(): def p022() -> None:
try: try:
with open('p022_names.txt', 'r', encoding='utf-8') as fp: with open('p022_names.txt', 'r', encoding='utf-8') as fp:
names = list(fp.readline().replace('"', '').split(',')) names = list(fp.readline().replace('"', '').split(','))
@ -24,7 +24,7 @@ def p022():
names.sort() names.sort()
sum_ = 0 _sum = 0
i = 1 i = 1
# Calculate the score of each name an multiply by its position. # Calculate the score of each name an multiply by its position.
@ -34,11 +34,11 @@ def p022():
for j in range(l): for j in range(l):
score = score + ord(name[j]) - ord('A') + 1 score = score + ord(name[j]) - ord('A') + 1
score = score * i score = score * i
sum_ = sum_ + score _sum = _sum + score
i = i + 1 i = i + 1
print('Project Euler, Problem 22') print('Project Euler, Problem 22')
print(f'Answer: {sum_}') print(f'Answer: {_sum}')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -15,12 +15,12 @@
from projecteuler import sum_of_divisors, timing from projecteuler import sum_of_divisors, timing
def is_abundant(n): def is_abundant(n: int) -> bool:
return sum_of_divisors(n) > n return sum_of_divisors(n) > n
@timing @timing
def p023(): def p023() -> None:
ab_nums = [False] * 28124 ab_nums = [False] * 28124
# Find all abundant numbers smaller than 28123. # Find all abundant numbers smaller than 28123.
@ -36,19 +36,20 @@ def p023():
if ab_nums[i]: if ab_nums[i]:
for j in range(i, 28123): for j in range(i, 28123):
if ab_nums[j]: if ab_nums[j]:
sum_ = i + j _sum = i + j
if sum_ <= 28123:
sums[sum_] = True
sum_ = 0 if _sum <= 28123:
sums[_sum] = True
_sum = 0
# Sum every number that was not found as a sum of two abundant numbers. # Sum every number that was not found as a sum of two abundant numbers.
for i in range(1, 28124): for i in range(1, 28124):
if not sums[i]: if not sums[i]:
sum_ = sum_ + i _sum = _sum + i
print('Project Euler, Problem 23') print('Project Euler, Problem 23')
print(f'Answer: {sum_}') print(f'Answer: {_sum}')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -13,7 +13,7 @@ from projecteuler import timing
@timing @timing
def p024(): def p024() -> None:
# Generate all the permutations in lexicographic order and get the millionth one. # Generate all the permutations in lexicographic order and get the millionth one.
perm = list(permutations('0123456789')) perm = list(permutations('0123456789'))
res = int(''.join(map(str, perm[999999]))) res = int(''.join(map(str, perm[999999])))

View File

@ -25,7 +25,7 @@ from projecteuler import timing
@timing @timing
def p025(): def p025() -> None:
fib1 = 1 fib1 = 1
fib2 = 1 fib2 = 1
fibn = fib1 + fib2 fibn = fib1 + fib2

View File

@ -20,8 +20,9 @@ from projecteuler import timing
@timing @timing
def p026(): def p026() -> None:
max_ = 0 _max = 0
max_n = -1
for i in range(2, 1000): for i in range(2, 1000):
j = i j = i
@ -50,8 +51,8 @@ def p026():
k = k * 10 k = k * 10
k = k + 9 k = k + 9
if n > max_: if n > _max:
max_ = n _max = n
max_n = i max_n = i
print('Project Euler, Problem 26') print('Project Euler, Problem 26')

View File

@ -24,8 +24,8 @@ from projecteuler import is_prime, timing
@timing @timing
def p027(): def p027() -> None:
max_ = 0 _max = 0
# Brute force approach, optimized by checking only values of b where b is prime. # Brute force approach, optimized by checking only values of b where b is prime.
for a in range(-999, 1000): for a in range(-999, 1000):
@ -44,8 +44,8 @@ def p027():
else: else:
break break
if count > max_: if count > _max:
max_ = count _max = count
save_a = a save_a = a
save_b = b save_b = b

View File

@ -16,7 +16,7 @@ from projecteuler import timing
@timing @timing
def p028(): def p028() -> None:
N = 1001 N = 1001
limit = N * N limit = N * N
@ -24,7 +24,7 @@ def p028():
i = 0 i = 0
j = 1 j = 1
step = 0 step = 0
sum_ = 1 _sum = 1
# Starting with the central 1, it's easy to see that the next four numbers in the diagonal # Starting with the central 1, it's easy to see that the next four numbers in the diagonal
# are 1+2, 1+2+2, 1+2+2+2 and 1+2+2+2+2, then for the next four number the step is increased # are 1+2, 1+2+2, 1+2+2+2 and 1+2+2+2+2, then for the next four number the step is increased
@ -34,11 +34,11 @@ def p028():
if i == 0: if i == 0:
step = step + 2 step = step + 2
j = j + step j = j + step
sum_ = sum_ + j _sum = _sum + j
i = (i + 1) % 4 i = (i + 1) % 4
print('Project Euler, Problem 28') print('Project Euler, Problem 28')
print(f'Answer: {sum_}') print(f'Answer: {_sum}')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -19,17 +19,17 @@ from projecteuler import timing
@timing @timing
def p029(): def p029() -> None:
powers = zeros(9801) _powers = zeros(9801)
# Generate all the powers # Generate all the powers
for i in range(2, 101): for i in range(2, 101):
a = i a = i
for j in range(2, 101): for j in range(2, 101):
powers[(i-2)*99+j-2] = a ** j _powers[(i-2)*99+j-2] = a ** j
# Sort the values and count the different values. # Sort the values and count the different values.
powers = list(powers) powers = list(_powers)
powers.sort() powers.sort()
count = 1 count = 1

View File

@ -16,7 +16,7 @@ from projecteuler import timing
@timing @timing
def p030(): def p030() -> None:
tot = 0 tot = 0
# Straightforward brute force approach. The limit is chosen considering that # Straightforward brute force approach. The limit is chosen considering that
@ -24,14 +24,14 @@ def p030():
# of 5th power of its digits. # of 5th power of its digits.
for i in range(10, 354295): for i in range(10, 354295):
j = i j = i
sum_ = 0 _sum = 0
while j > 0: while j > 0:
digit = j % 10 digit = j % 10
sum_ = sum_ + digit ** 5 _sum = _sum + digit ** 5
j = j // 10 j = j // 10
if sum_ == i: if _sum == i:
tot = tot + i tot = tot + i
print('Project Euler, Problem 30') print('Project Euler, Problem 30')