Add type hints in problems 21-30
This commit is contained in:
parent
f8b5260349
commit
48ab60733d
@ -13,8 +13,8 @@ from projecteuler import sum_of_divisors, timing
|
||||
|
||||
|
||||
@timing
|
||||
def p021():
|
||||
sum_ = 0
|
||||
def p021() -> None:
|
||||
_sum = 0
|
||||
|
||||
for i in range(2, 10000):
|
||||
# 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,
|
||||
# sum the pair of numbers and add it to the total.
|
||||
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(f'Answer: {sum_}')
|
||||
print(f'Answer: {_sum}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -14,7 +14,7 @@ from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p022():
|
||||
def p022() -> None:
|
||||
try:
|
||||
with open('p022_names.txt', 'r', encoding='utf-8') as fp:
|
||||
names = list(fp.readline().replace('"', '').split(','))
|
||||
@ -24,7 +24,7 @@ def p022():
|
||||
|
||||
names.sort()
|
||||
|
||||
sum_ = 0
|
||||
_sum = 0
|
||||
i = 1
|
||||
|
||||
# Calculate the score of each name an multiply by its position.
|
||||
@ -34,11 +34,11 @@ def p022():
|
||||
for j in range(l):
|
||||
score = score + ord(name[j]) - ord('A') + 1
|
||||
score = score * i
|
||||
sum_ = sum_ + score
|
||||
_sum = _sum + score
|
||||
i = i + 1
|
||||
|
||||
print('Project Euler, Problem 22')
|
||||
print(f'Answer: {sum_}')
|
||||
print(f'Answer: {_sum}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -15,12 +15,12 @@
|
||||
from projecteuler import sum_of_divisors, timing
|
||||
|
||||
|
||||
def is_abundant(n):
|
||||
def is_abundant(n: int) -> bool:
|
||||
return sum_of_divisors(n) > n
|
||||
|
||||
|
||||
@timing
|
||||
def p023():
|
||||
def p023() -> None:
|
||||
ab_nums = [False] * 28124
|
||||
|
||||
# Find all abundant numbers smaller than 28123.
|
||||
@ -36,19 +36,20 @@ def p023():
|
||||
if ab_nums[i]:
|
||||
for j in range(i, 28123):
|
||||
if ab_nums[j]:
|
||||
sum_ = i + j
|
||||
if sum_ <= 28123:
|
||||
sums[sum_] = True
|
||||
_sum = i + j
|
||||
|
||||
sum_ = 0
|
||||
if _sum <= 28123:
|
||||
sums[_sum] = True
|
||||
|
||||
_sum = 0
|
||||
|
||||
# Sum every number that was not found as a sum of two abundant numbers.
|
||||
for i in range(1, 28124):
|
||||
if not sums[i]:
|
||||
sum_ = sum_ + i
|
||||
_sum = _sum + i
|
||||
|
||||
print('Project Euler, Problem 23')
|
||||
print(f'Answer: {sum_}')
|
||||
print(f'Answer: {_sum}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -13,7 +13,7 @@ from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p024():
|
||||
def p024() -> None:
|
||||
# Generate all the permutations in lexicographic order and get the millionth one.
|
||||
perm = list(permutations('0123456789'))
|
||||
res = int(''.join(map(str, perm[999999])))
|
||||
|
@ -25,7 +25,7 @@ from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p025():
|
||||
def p025() -> None:
|
||||
fib1 = 1
|
||||
fib2 = 1
|
||||
fibn = fib1 + fib2
|
||||
|
@ -20,8 +20,9 @@ from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p026():
|
||||
max_ = 0
|
||||
def p026() -> None:
|
||||
_max = 0
|
||||
max_n = -1
|
||||
|
||||
for i in range(2, 1000):
|
||||
j = i
|
||||
@ -50,8 +51,8 @@ def p026():
|
||||
k = k * 10
|
||||
k = k + 9
|
||||
|
||||
if n > max_:
|
||||
max_ = n
|
||||
if n > _max:
|
||||
_max = n
|
||||
max_n = i
|
||||
|
||||
print('Project Euler, Problem 26')
|
||||
|
@ -24,8 +24,8 @@ from projecteuler import is_prime, timing
|
||||
|
||||
|
||||
@timing
|
||||
def p027():
|
||||
max_ = 0
|
||||
def p027() -> None:
|
||||
_max = 0
|
||||
|
||||
# Brute force approach, optimized by checking only values of b where b is prime.
|
||||
for a in range(-999, 1000):
|
||||
@ -44,8 +44,8 @@ def p027():
|
||||
else:
|
||||
break
|
||||
|
||||
if count > max_:
|
||||
max_ = count
|
||||
if count > _max:
|
||||
_max = count
|
||||
save_a = a
|
||||
save_b = b
|
||||
|
||||
|
@ -16,7 +16,7 @@ from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p028():
|
||||
def p028() -> None:
|
||||
N = 1001
|
||||
|
||||
limit = N * N
|
||||
@ -24,7 +24,7 @@ def p028():
|
||||
i = 0
|
||||
j = 1
|
||||
step = 0
|
||||
sum_ = 1
|
||||
_sum = 1
|
||||
|
||||
# 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
|
||||
@ -34,11 +34,11 @@ def p028():
|
||||
if i == 0:
|
||||
step = step + 2
|
||||
j = j + step
|
||||
sum_ = sum_ + j
|
||||
_sum = _sum + j
|
||||
i = (i + 1) % 4
|
||||
|
||||
print('Project Euler, Problem 28')
|
||||
print(f'Answer: {sum_}')
|
||||
print(f'Answer: {_sum}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -19,17 +19,17 @@ from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p029():
|
||||
powers = zeros(9801)
|
||||
def p029() -> None:
|
||||
_powers = zeros(9801)
|
||||
|
||||
# Generate all the powers
|
||||
for i in range(2, 101):
|
||||
a = i
|
||||
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.
|
||||
powers = list(powers)
|
||||
powers = list(_powers)
|
||||
powers.sort()
|
||||
|
||||
count = 1
|
||||
|
@ -16,7 +16,7 @@ from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p030():
|
||||
def p030() -> None:
|
||||
tot = 0
|
||||
|
||||
# Straightforward brute force approach. The limit is chosen considering that
|
||||
@ -24,14 +24,14 @@ def p030():
|
||||
# of 5th power of its digits.
|
||||
for i in range(10, 354295):
|
||||
j = i
|
||||
sum_ = 0
|
||||
_sum = 0
|
||||
|
||||
while j > 0:
|
||||
digit = j % 10
|
||||
sum_ = sum_ + digit ** 5
|
||||
_sum = _sum + digit ** 5
|
||||
j = j // 10
|
||||
|
||||
if sum_ == i:
|
||||
if _sum == i:
|
||||
tot = tot + i
|
||||
|
||||
print('Project Euler, Problem 30')
|
||||
|
Loading…
x
Reference in New Issue
Block a user