diff --git a/Python/p031.py b/Python/p031.py index 0a617c3..f563da4 100644 --- a/Python/p031.py +++ b/Python/p031.py @@ -10,13 +10,16 @@ # # How many different ways can £2 be made using any number of coins? +from typing import List + from projecteuler import timing # 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): value = value + coins[j] + if value == 200: return n + 1 @@ -30,7 +33,7 @@ def count(coins, value, n, i): @timing -def p031(): +def p031() -> None: coins = [1, 2, 5, 10, 20, 50, 100, 200] n = count(coins, 0, 0, 0) diff --git a/Python/p032.py b/Python/p032.py index 4b32281..a869ea9 100644 --- a/Python/p032.py +++ b/Python/p032.py @@ -16,7 +16,7 @@ from projecteuler import is_pandigital, timing @timing -def p032(): +def p032() -> None: n = 0 # Initially I used a bigger array, but printing the resulting products # shows that 10 values are sufficient. @@ -70,14 +70,14 @@ def p032(): # Sort the found products to easily see if there are duplicates. products = np.sort(products[:n]) - sum_ = products[0] + _sum = products[0] for i in range(1, n): if products[i] != products[i-1]: - sum_ = sum_ + products[i] + _sum = _sum + products[i] print('Project Euler, Problem 32') - print(f'Answer: {sum_}') + print(f'Answer: {_sum}') if __name__ == '__main__': diff --git a/Python/p033.py b/Python/p033.py index 0532362..9ffcc4d 100644 --- a/Python/p033.py +++ b/Python/p033.py @@ -16,7 +16,7 @@ from projecteuler import timing @timing -def p033(): +def p033() -> None: prod_n = 1 prod_d = 1 diff --git a/Python/p034.py b/Python/p034.py index 52f66ed..3b057ec 100644 --- a/Python/p034.py +++ b/Python/p034.py @@ -14,9 +14,9 @@ from projecteuler import timing @timing -def p034(): +def p034() -> None: a = 10 - sum_ = 0 + _sum = 0 factorials = ones(10, int) # Pre-calculate factorials of each digit from 0 to 9. @@ -34,12 +34,12 @@ def p034(): sum_f = sum_f + factorials[digit] if a == sum_f: - sum_ = sum_ + a + _sum = _sum + a a = a + 1 print('Project Euler, Problem 34') - print(f'Answer: {sum_}') + print(f'Answer: {_sum}') if __name__ == '__main__': diff --git a/Python/p035.py b/Python/p035.py index 9190de2..6602256 100644 --- a/Python/p035.py +++ b/Python/p035.py @@ -14,7 +14,7 @@ N = 1000000 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 primes[n] == 0: return False @@ -31,6 +31,7 @@ def is_circular_prime(n): while tmp > 0: if tmp % 2 == 0: return False + # Count the number of digits. count = count + 1 tmp = tmp // 10 @@ -46,7 +47,7 @@ def is_circular_prime(n): @timing -def p035(): +def p035() -> None: count = 13 # Calculate all primes below one million, then check if they're circular. diff --git a/Python/p036.py b/Python/p036.py index 127e1ae..a549b05 100644 --- a/Python/p036.py +++ b/Python/p036.py @@ -10,20 +10,20 @@ from projecteuler import is_palindrome, timing @timing -def p036(): +def p036() -> None: N = 1000000 - sum_ = 0 + _sum = 0 # Brute force approach. For every number below 1 million, # check if they're palindrome in base 2 and 10 using the # function implemented in projecteuler.c. for i in range(1, N): if is_palindrome(i, 10) and is_palindrome(i, 2): - sum_ = sum_ + i + _sum = _sum + i print('Project Euler, Problem 36') - print(f'Answer: {sum_}') + print(f'Answer: {_sum}') if __name__ == '__main__': diff --git a/Python/p037.py b/Python/p037.py index e4279e2..e236f94 100644 --- a/Python/p037.py +++ b/Python/p037.py @@ -10,7 +10,7 @@ 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 # not truncatable primes. if n < 11 or not is_prime(n): @@ -23,6 +23,7 @@ def is_tr_prime(n): while tmp > 0: if not is_prime(tmp): return False + 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 @@ -33,6 +34,7 @@ def is_tr_prime(n): while tmp != n: if not is_prime(tmp): return False + i = i * 10 tmp = n % i @@ -41,20 +43,20 @@ def is_tr_prime(n): @timing -def p037(): +def p037() -> None: i = 0 n = 1 - sum_ = 0 + _sum = 0 # Check every number until 11 truncatable primes are found. while i < 11: if is_tr_prime(n): - sum_ = sum_ + n + _sum = _sum + n i = i + 1 n = n + 1 print('Project Euler, Problem 37') - print(f'Answer: {sum_}') + print(f'Answer: {_sum}') if __name__ == '__main__': diff --git a/Python/p038.py b/Python/p038.py index 2464e2f..e359dd0 100644 --- a/Python/p038.py +++ b/Python/p038.py @@ -17,8 +17,8 @@ from projecteuler import is_pandigital, timing @timing -def p038(): - max_ = 0 +def p038() -> None: + _max = 0 # A brute force approach is used, starting with 1 and multiplying # the number by 1, 2 etc., concatenating the results, checking if @@ -35,8 +35,8 @@ def p038(): n = n + tmp j = j + 1 - if n > max_ and is_pandigital(n, 9): - max_ = n + if n > _max and is_pandigital(n, 9): + _max = n if i * j < 10: n = n * 10 elif i * j < 100: @@ -52,7 +52,7 @@ def p038(): break print('Project Euler, Problem 38') - print(f'Answer: {max_}') + print(f'Answer: {_max}') if __name__ == '__main__': diff --git a/Python/p039.py b/Python/p039.py index b539848..fdaf2d8 100644 --- a/Python/p039.py +++ b/Python/p039.py @@ -12,8 +12,9 @@ from projecteuler import timing @timing -def p039(): - max_ = 0 +def p039() -> None: + _max = 0 + res = 0 savedc = zeros(1000, int) # 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, # save the new maximum and the value of p. - if count > max_: - max_ = count + if count > _max: + _max = count res = p print('Project Euler, Problem 39') diff --git a/Python/p040.py b/Python/p040.py index 59b9a88..c3bab77 100644 --- a/Python/p040.py +++ b/Python/p040.py @@ -16,7 +16,7 @@ from projecteuler import timing @timing -def p040(): +def p040() -> None: digits = zeros(1000005, int) i = 1 value = 1