Add type hints in problems 41-50

This commit is contained in:
daniele 2024-09-29 19:53:11 +02:00
parent 3a4f85db04
commit 44fd50f067
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
10 changed files with 35 additions and 33 deletions

View File

@ -9,7 +9,7 @@ from projecteuler import is_pandigital, is_prime, timing
@timing @timing
def p041(): def p041() -> None:
# 8- and 9-digit pandigital numbers can't be prime, because # 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 # 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 # also divisible by 3, and therefore the whole number is divisible

View File

@ -15,13 +15,14 @@ import sys
from projecteuler import timing from projecteuler import timing
def is_triang(n): def is_triang(n: int) -> bool:
i = 1 i = 1
j = 1 j = 1
while j <= n: while j <= n:
if n == j: if n == j:
return True return True
i = i + 1 i = i + 1
j = j + i j = j + i
@ -29,7 +30,7 @@ def is_triang(n):
@timing @timing
def p042(): def p042() -> None:
try: try:
with open('p042_words.txt', 'r', encoding='utf-8') as fp: with open('p042_words.txt', 'r', encoding='utf-8') as fp:
words = list(fp.readline().replace('"', '').split(',')) words = list(fp.readline().replace('"', '').split(','))

View File

@ -14,14 +14,14 @@
# d8d9d10=289 is divisible by 17 # d8d9d10=289 is divisible by 17
# #
# Find the sum of all 0 to 9 pandigital numbers with this property. # Find the sum of all 0 to 9 pandigital numbers with this property.
from typing import Tuple
from itertools import permutations from itertools import permutations
from projecteuler import timing from projecteuler import timing
# Function to check if the value has the desired property. # 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]) value = int(n[1]) * 100 + int(n[2]) * 10 + int(n[3])
if value % 2 != 0: if value % 2 != 0:
@ -65,15 +65,15 @@ def p043():
# Find all the permutations # Find all the permutations
perm = list(permutations('0123456789')) perm = list(permutations('0123456789'))
sum_ = 0 _sum = 0
# For each permutation, check if it has the required property # For each permutation, check if it has the required property
for i in perm: for i in perm:
if has_property(i): if has_property(i):
sum_ = sum_ + int(''.join(map(str, i))) _sum = _sum + int(''.join(map(str, i)))
print('Project Euler, Problem 43') print('Project Euler, Problem 43')
print(f'Answer: {sum_}') print(f'Answer: {_sum}')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -13,7 +13,7 @@ from projecteuler import is_pentagonal, timing
@timing @timing
def p044(): def p044() -> None:
found = 0 found = 0
n = 2 n = 2

View File

@ -14,7 +14,7 @@ from projecteuler import is_pentagonal, timing
@timing @timing
def p045(): def p045() -> None:
found = 0 found = 0
i = 143 i = 143

View File

@ -20,7 +20,7 @@ N = 10000
primes = sieve(N) primes = sieve(N)
def goldbach(n): def goldbach(n: int) -> bool:
# Check every prime smaller than n. # Check every prime smaller than n.
for i in range(3, n, 2): for i in range(3, n, 2):
if primes[i] == 1: if primes[i] == 1:
@ -44,7 +44,7 @@ def goldbach(n):
@timing @timing
def p046(): def p046() -> None:
found = 0 found = 0
i = 3 i = 3

View File

@ -12,13 +12,14 @@
# 646 = 2 × 17 × 19. # 646 = 2 × 17 × 19.
# #
# Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers? # 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 from projecteuler import timing
# Function using a modified sieve of Eratosthenes to count # Function using a modified sieve of Eratosthenes to count
# the distinct prime factors of each number. # the distinct prime factors of each number.
def count_factors(n): def count_factors(n: int) -> List[int]:
factors = [0] * n factors = [0] * n
i = 2 i = 2
@ -32,7 +33,7 @@ def count_factors(n):
@timing @timing
def p047(): def p047() -> None:
N = 150000 N = 150000
factors = count_factors(N) factors = count_factors(N)

View File

@ -8,16 +8,16 @@ from projecteuler import timing
@timing @timing
def p048(): def p048() -> None:
sum_ = 0 _sum = 0
# Simply calculate the sum of the powers # Simply calculate the sum of the powers
for i in range(1, 1001): for i in range(1, 1001):
power = i ** i power = i ** i
sum_ = sum_ + power _sum = _sum + power
print('Project Euler, Problem 48') print('Project Euler, Problem 48')
print(f'Answer: {str(sum_)[-10:]}') print(f'Answer: {str(_sum)[-10:]}')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -12,7 +12,7 @@ from projecteuler import sieve, timing
@timing @timing
def p049(): def p049() -> None:
N = 10000 N = 10000
primes = sieve(N) primes = sieve(N)

View File

@ -14,12 +14,12 @@ from projecteuler import sieve, timing
@timing @timing
def p050(): def p050() -> None:
N = 1000000 N = 1000000
primes = sieve(N) primes = sieve(N)
max_ = 0 _max = 0
max_p = 0 max_p = 0
# Starting from a prime i, add consecutive primes until the # Starting from a prime i, add consecutive primes until the
@ -31,32 +31,32 @@ def p050():
i = 2 i = 2
j = i + 1 j = i + 1
count = 1 count = 1
sum_ = i _sum = i
while j < N and sum_ < N: while j < N and _sum < N:
if primes[j] == 1: if primes[j] == 1:
sum_ = sum_ + j _sum = _sum + j
count = count + 1 count = count + 1
if sum_ < N and primes[sum_] == 1 and count > max_: if _sum < N and primes[_sum] == 1 and count > _max:
max_ = count _max = count
max_p = sum max_p = _sum
j = j + 1 j = j + 1
for i in range(3, N, 2): for i in range(3, N, 2):
if primes[i] == 1: if primes[i] == 1:
count = 1 count = 1
sum_ = i _sum = i
j = i + 2 j = i + 2
while j < N and sum_ < N: while j < N and _sum < N:
if primes[j] == 1: if primes[j] == 1:
sum_ = sum_ + j _sum = _sum + j
count = count + 1 count = count + 1
if sum_ < N and primes[sum_] == 1 and count > max_: if _sum < N and primes[_sum] == 1 and count > _max:
max_ = count _max = count
max_p = sum_ max_p = _sum
j = j + 2 j = j + 2