Add type hints in problems 31-40
This commit is contained in:
parent
48ab60733d
commit
3a4f85db04
@ -10,13 +10,16 @@
|
|||||||
#
|
#
|
||||||
# How many different ways can £2 be made using any number of coins?
|
# How many different ways can £2 be made using any number of coins?
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from projecteuler import timing
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
# Simple recursive function that tries every combination.
|
# Simple recursive function that tries every combination.
|
||||||
def count(coins, value, n, i):
|
def count(coins: List[int], value: int, n: int, i: int) -> int:
|
||||||
for j in range(i, 8):
|
for j in range(i, 8):
|
||||||
value = value + coins[j]
|
value = value + coins[j]
|
||||||
|
|
||||||
if value == 200:
|
if value == 200:
|
||||||
return n + 1
|
return n + 1
|
||||||
|
|
||||||
@ -30,7 +33,7 @@ def count(coins, value, n, i):
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p031():
|
def p031() -> None:
|
||||||
coins = [1, 2, 5, 10, 20, 50, 100, 200]
|
coins = [1, 2, 5, 10, 20, 50, 100, 200]
|
||||||
|
|
||||||
n = count(coins, 0, 0, 0)
|
n = count(coins, 0, 0, 0)
|
||||||
|
@ -16,7 +16,7 @@ from projecteuler import is_pandigital, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p032():
|
def p032() -> None:
|
||||||
n = 0
|
n = 0
|
||||||
# Initially I used a bigger array, but printing the resulting products
|
# Initially I used a bigger array, but printing the resulting products
|
||||||
# shows that 10 values are sufficient.
|
# shows that 10 values are sufficient.
|
||||||
@ -70,14 +70,14 @@ def p032():
|
|||||||
# Sort the found products to easily see if there are duplicates.
|
# Sort the found products to easily see if there are duplicates.
|
||||||
products = np.sort(products[:n])
|
products = np.sort(products[:n])
|
||||||
|
|
||||||
sum_ = products[0]
|
_sum = products[0]
|
||||||
|
|
||||||
for i in range(1, n):
|
for i in range(1, n):
|
||||||
if products[i] != products[i-1]:
|
if products[i] != products[i-1]:
|
||||||
sum_ = sum_ + products[i]
|
_sum = _sum + products[i]
|
||||||
|
|
||||||
print('Project Euler, Problem 32')
|
print('Project Euler, Problem 32')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -16,7 +16,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p033():
|
def p033() -> None:
|
||||||
prod_n = 1
|
prod_n = 1
|
||||||
prod_d = 1
|
prod_d = 1
|
||||||
|
|
||||||
|
@ -14,9 +14,9 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p034():
|
def p034() -> None:
|
||||||
a = 10
|
a = 10
|
||||||
sum_ = 0
|
_sum = 0
|
||||||
factorials = ones(10, int)
|
factorials = ones(10, int)
|
||||||
|
|
||||||
# Pre-calculate factorials of each digit from 0 to 9.
|
# Pre-calculate factorials of each digit from 0 to 9.
|
||||||
@ -34,12 +34,12 @@ def p034():
|
|||||||
sum_f = sum_f + factorials[digit]
|
sum_f = sum_f + factorials[digit]
|
||||||
|
|
||||||
if a == sum_f:
|
if a == sum_f:
|
||||||
sum_ = sum_ + a
|
_sum = _sum + a
|
||||||
|
|
||||||
a = a + 1
|
a = a + 1
|
||||||
|
|
||||||
print('Project Euler, Problem 34')
|
print('Project Euler, Problem 34')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -14,7 +14,7 @@ N = 1000000
|
|||||||
primes = sieve(N)
|
primes = sieve(N)
|
||||||
|
|
||||||
|
|
||||||
def is_circular_prime(n):
|
def is_circular_prime(n: int) -> bool:
|
||||||
# If n is not prime, it's obviously not a circular prime.
|
# If n is not prime, it's obviously not a circular prime.
|
||||||
if primes[n] == 0:
|
if primes[n] == 0:
|
||||||
return False
|
return False
|
||||||
@ -31,6 +31,7 @@ def is_circular_prime(n):
|
|||||||
while tmp > 0:
|
while tmp > 0:
|
||||||
if tmp % 2 == 0:
|
if tmp % 2 == 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Count the number of digits.
|
# Count the number of digits.
|
||||||
count = count + 1
|
count = count + 1
|
||||||
tmp = tmp // 10
|
tmp = tmp // 10
|
||||||
@ -46,7 +47,7 @@ def is_circular_prime(n):
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p035():
|
def p035() -> None:
|
||||||
count = 13
|
count = 13
|
||||||
|
|
||||||
# Calculate all primes below one million, then check if they're circular.
|
# Calculate all primes below one million, then check if they're circular.
|
||||||
|
@ -10,20 +10,20 @@ from projecteuler import is_palindrome, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p036():
|
def p036() -> None:
|
||||||
N = 1000000
|
N = 1000000
|
||||||
|
|
||||||
sum_ = 0
|
_sum = 0
|
||||||
|
|
||||||
# Brute force approach. For every number below 1 million,
|
# Brute force approach. For every number below 1 million,
|
||||||
# check if they're palindrome in base 2 and 10 using the
|
# check if they're palindrome in base 2 and 10 using the
|
||||||
# function implemented in projecteuler.c.
|
# function implemented in projecteuler.c.
|
||||||
for i in range(1, N):
|
for i in range(1, N):
|
||||||
if is_palindrome(i, 10) and is_palindrome(i, 2):
|
if is_palindrome(i, 10) and is_palindrome(i, 2):
|
||||||
sum_ = sum_ + i
|
_sum = _sum + i
|
||||||
|
|
||||||
print('Project Euler, Problem 36')
|
print('Project Euler, Problem 36')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
from projecteuler import is_prime, timing
|
from projecteuler import is_prime, timing
|
||||||
|
|
||||||
|
|
||||||
def is_tr_prime(n):
|
def is_tr_prime(n: int) -> bool:
|
||||||
# One-digit numbers and non-prime numbers are
|
# One-digit numbers and non-prime numbers are
|
||||||
# not truncatable primes.
|
# not truncatable primes.
|
||||||
if n < 11 or not is_prime(n):
|
if n < 11 or not is_prime(n):
|
||||||
@ -23,6 +23,7 @@ def is_tr_prime(n):
|
|||||||
while tmp > 0:
|
while tmp > 0:
|
||||||
if not is_prime(tmp):
|
if not is_prime(tmp):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
tmp = tmp // 10
|
tmp = tmp // 10
|
||||||
|
|
||||||
# Starting from the last digit, check if it's prime, then add back one digit at a time on the left and check if it
|
# Starting from the last digit, check if it's prime, then add back one digit at a time on the left and check if it
|
||||||
@ -33,6 +34,7 @@ def is_tr_prime(n):
|
|||||||
while tmp != n:
|
while tmp != n:
|
||||||
if not is_prime(tmp):
|
if not is_prime(tmp):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
i = i * 10
|
i = i * 10
|
||||||
tmp = n % i
|
tmp = n % i
|
||||||
|
|
||||||
@ -41,20 +43,20 @@ def is_tr_prime(n):
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p037():
|
def p037() -> None:
|
||||||
i = 0
|
i = 0
|
||||||
n = 1
|
n = 1
|
||||||
sum_ = 0
|
_sum = 0
|
||||||
|
|
||||||
# Check every number until 11 truncatable primes are found.
|
# Check every number until 11 truncatable primes are found.
|
||||||
while i < 11:
|
while i < 11:
|
||||||
if is_tr_prime(n):
|
if is_tr_prime(n):
|
||||||
sum_ = sum_ + n
|
_sum = _sum + n
|
||||||
i = i + 1
|
i = i + 1
|
||||||
n = n + 1
|
n = n + 1
|
||||||
|
|
||||||
print('Project Euler, Problem 37')
|
print('Project Euler, Problem 37')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -17,8 +17,8 @@ from projecteuler import is_pandigital, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p038():
|
def p038() -> None:
|
||||||
max_ = 0
|
_max = 0
|
||||||
|
|
||||||
# A brute force approach is used, starting with 1 and multiplying
|
# A brute force approach is used, starting with 1 and multiplying
|
||||||
# the number by 1, 2 etc., concatenating the results, checking if
|
# the number by 1, 2 etc., concatenating the results, checking if
|
||||||
@ -35,8 +35,8 @@ def p038():
|
|||||||
n = n + tmp
|
n = n + tmp
|
||||||
j = j + 1
|
j = j + 1
|
||||||
|
|
||||||
if n > max_ and is_pandigital(n, 9):
|
if n > _max and is_pandigital(n, 9):
|
||||||
max_ = n
|
_max = n
|
||||||
if i * j < 10:
|
if i * j < 10:
|
||||||
n = n * 10
|
n = n * 10
|
||||||
elif i * j < 100:
|
elif i * j < 100:
|
||||||
@ -52,7 +52,7 @@ def p038():
|
|||||||
break
|
break
|
||||||
|
|
||||||
print('Project Euler, Problem 38')
|
print('Project Euler, Problem 38')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {_max}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -12,8 +12,9 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p039():
|
def p039() -> None:
|
||||||
max_ = 0
|
_max = 0
|
||||||
|
res = 0
|
||||||
savedc = zeros(1000, int)
|
savedc = zeros(1000, int)
|
||||||
|
|
||||||
# Start with p=12 (the smallest pythagorean triplet is (3,4,5) and 3+4+5=12.
|
# Start with p=12 (the smallest pythagorean triplet is (3,4,5) and 3+4+5=12.
|
||||||
@ -59,8 +60,8 @@ def p039():
|
|||||||
|
|
||||||
# If the current value is greater than the maximum,
|
# If the current value is greater than the maximum,
|
||||||
# save the new maximum and the value of p.
|
# save the new maximum and the value of p.
|
||||||
if count > max_:
|
if count > _max:
|
||||||
max_ = count
|
_max = count
|
||||||
res = p
|
res = p
|
||||||
|
|
||||||
print('Project Euler, Problem 39')
|
print('Project Euler, Problem 39')
|
||||||
|
@ -16,7 +16,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p040():
|
def p040() -> None:
|
||||||
digits = zeros(1000005, int)
|
digits = zeros(1000005, int)
|
||||||
i = 1
|
i = 1
|
||||||
value = 1
|
value = 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user