From 44fd50f067f8db45222a48d12458650b4a867059 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Sun, 29 Sep 2024 19:53:11 +0200 Subject: [PATCH] Add type hints in problems 41-50 --- Python/p041.py | 2 +- Python/p042.py | 5 +++-- Python/p043.py | 10 +++++----- Python/p044.py | 2 +- Python/p045.py | 2 +- Python/p046.py | 4 ++-- Python/p047.py | 5 +++-- Python/p048.py | 8 ++++---- Python/p049.py | 2 +- Python/p050.py | 28 ++++++++++++++-------------- 10 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Python/p041.py b/Python/p041.py index cc3075f..593947a 100644 --- a/Python/p041.py +++ b/Python/p041.py @@ -9,7 +9,7 @@ from projecteuler import is_pandigital, is_prime, timing @timing -def p041(): +def p041() -> None: # 8- and 9-digit pandigital numbers can't be prime, because # 1+2+...+8=36, which is divisible by 3, and 36+9=45 which is # also divisible by 3, and therefore the whole number is divisible diff --git a/Python/p042.py b/Python/p042.py index 1b1151e..c2a780c 100644 --- a/Python/p042.py +++ b/Python/p042.py @@ -15,13 +15,14 @@ import sys from projecteuler import timing -def is_triang(n): +def is_triang(n: int) -> bool: i = 1 j = 1 while j <= n: if n == j: return True + i = i + 1 j = j + i @@ -29,7 +30,7 @@ def is_triang(n): @timing -def p042(): +def p042() -> None: try: with open('p042_words.txt', 'r', encoding='utf-8') as fp: words = list(fp.readline().replace('"', '').split(',')) diff --git a/Python/p043.py b/Python/p043.py index 84ba91e..b66be18 100644 --- a/Python/p043.py +++ b/Python/p043.py @@ -14,14 +14,14 @@ # d8d9d10=289 is divisible by 17 # # Find the sum of all 0 to 9 pandigital numbers with this property. - +from typing import Tuple from itertools import permutations from projecteuler import timing # Function to check if the value has the desired property. -def has_property(n): +def has_property(n: Tuple[str, ...]) -> bool: value = int(n[1]) * 100 + int(n[2]) * 10 + int(n[3]) if value % 2 != 0: @@ -65,15 +65,15 @@ def p043(): # Find all the permutations perm = list(permutations('0123456789')) - sum_ = 0 + _sum = 0 # For each permutation, check if it has the required property for i in perm: if has_property(i): - sum_ = sum_ + int(''.join(map(str, i))) + _sum = _sum + int(''.join(map(str, i))) print('Project Euler, Problem 43') - print(f'Answer: {sum_}') + print(f'Answer: {_sum}') if __name__ == '__main__': diff --git a/Python/p044.py b/Python/p044.py index 7949196..b7c4349 100644 --- a/Python/p044.py +++ b/Python/p044.py @@ -13,7 +13,7 @@ from projecteuler import is_pentagonal, timing @timing -def p044(): +def p044() -> None: found = 0 n = 2 diff --git a/Python/p045.py b/Python/p045.py index 2f827be..2a31236 100644 --- a/Python/p045.py +++ b/Python/p045.py @@ -14,7 +14,7 @@ from projecteuler import is_pentagonal, timing @timing -def p045(): +def p045() -> None: found = 0 i = 143 diff --git a/Python/p046.py b/Python/p046.py index b959951..8b61f56 100644 --- a/Python/p046.py +++ b/Python/p046.py @@ -20,7 +20,7 @@ N = 10000 primes = sieve(N) -def goldbach(n): +def goldbach(n: int) -> bool: # Check every prime smaller than n. for i in range(3, n, 2): if primes[i] == 1: @@ -44,7 +44,7 @@ def goldbach(n): @timing -def p046(): +def p046() -> None: found = 0 i = 3 diff --git a/Python/p047.py b/Python/p047.py index c9ff23a..47f4d67 100644 --- a/Python/p047.py +++ b/Python/p047.py @@ -12,13 +12,14 @@ # 646 = 2 × 17 × 19. # # Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers? +from typing import List from projecteuler import timing # Function using a modified sieve of Eratosthenes to count # the distinct prime factors of each number. -def count_factors(n): +def count_factors(n: int) -> List[int]: factors = [0] * n i = 2 @@ -32,7 +33,7 @@ def count_factors(n): @timing -def p047(): +def p047() -> None: N = 150000 factors = count_factors(N) diff --git a/Python/p048.py b/Python/p048.py index d93ba3d..68e54bf 100644 --- a/Python/p048.py +++ b/Python/p048.py @@ -8,16 +8,16 @@ from projecteuler import timing @timing -def p048(): - sum_ = 0 +def p048() -> None: + _sum = 0 # Simply calculate the sum of the powers for i in range(1, 1001): power = i ** i - sum_ = sum_ + power + _sum = _sum + power print('Project Euler, Problem 48') - print(f'Answer: {str(sum_)[-10:]}') + print(f'Answer: {str(_sum)[-10:]}') if __name__ == '__main__': diff --git a/Python/p049.py b/Python/p049.py index c79e939..48d23d8 100644 --- a/Python/p049.py +++ b/Python/p049.py @@ -12,7 +12,7 @@ from projecteuler import sieve, timing @timing -def p049(): +def p049() -> None: N = 10000 primes = sieve(N) diff --git a/Python/p050.py b/Python/p050.py index c4c765f..1760ef3 100644 --- a/Python/p050.py +++ b/Python/p050.py @@ -14,12 +14,12 @@ from projecteuler import sieve, timing @timing -def p050(): +def p050() -> None: N = 1000000 primes = sieve(N) - max_ = 0 + _max = 0 max_p = 0 # Starting from a prime i, add consecutive primes until the @@ -31,32 +31,32 @@ def p050(): i = 2 j = i + 1 count = 1 - sum_ = i + _sum = i - while j < N and sum_ < N: + while j < N and _sum < N: if primes[j] == 1: - sum_ = sum_ + j + _sum = _sum + j count = count + 1 - if sum_ < N and primes[sum_] == 1 and count > max_: - max_ = count - max_p = sum + if _sum < N and primes[_sum] == 1 and count > _max: + _max = count + max_p = _sum j = j + 1 for i in range(3, N, 2): if primes[i] == 1: count = 1 - sum_ = i + _sum = i j = i + 2 - while j < N and sum_ < N: + while j < N and _sum < N: if primes[j] == 1: - sum_ = sum_ + j + _sum = _sum + j count = count + 1 - if sum_ < N and primes[sum_] == 1 and count > max_: - max_ = count - max_p = sum_ + if _sum < N and primes[_sum] == 1 and count > _max: + _max = count + max_p = _sum j = j + 2