Add type hints in problems 51-53

This commit is contained in:
daniele 2024-11-10 10:09:39 +01:00
parent 0f27c7f65a
commit 491eb89201
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
3 changed files with 11 additions and 9 deletions

View File

@ -17,7 +17,7 @@ N = 1000000
primes = sieve(N) primes = sieve(N)
def count_digit(n, d): def count_digit(n: int, d: int) -> int:
count = 0 count = 0
while n > 0: while n > 0:
@ -28,10 +28,10 @@ def count_digit(n, d):
return count return count
def replace(n): def replace(n: int) -> int:
n_string = list(str(n)) n_string = list(str(n))
l = len(n_string) l = len(n_string)
max_ = 0 _max = 0
for i in range(l-3): for i in range(l-3):
for j in range(i+1, l-2): for j in range(i+1, l-2):
@ -54,16 +54,16 @@ def replace(n):
if primes[n_replaced] == 1: if primes[n_replaced] == 1:
if count == 0 and n_replaced != n: if count == 0 and n_replaced != n:
continue continue
count = count + 1 count = count + 1
if count > max_: _max = max(_max, count)
max_ = count
return max_ return _max
@timing @timing
def p051(): def p051() -> None:
# Checking only odd numbers with at least 4 digits. # Checking only odd numbers with at least 4 digits.
i = 1001 i = 1001
@ -78,6 +78,7 @@ def p051():
count_digit(i, 2) >= 3: count_digit(i, 2) >= 3:
if primes[i] == 1 and replace(i) == 8: if primes[i] == 1 and replace(i) == 8:
break break
i = i + 2 i = i + 2
print('Project Euler, Problem 51') print('Project Euler, Problem 51')

View File

@ -8,7 +8,7 @@ from projecteuler import timing
@timing @timing
def p052(): def p052() -> None:
i = 1 i = 1
# Brute force approach, try every integer number until the desired one is found. # Brute force approach, try every integer number until the desired one is found.
@ -19,6 +19,7 @@ def p052():
''.join(sorted(str(i))) == ''.join(sorted(str(5*i))) and\ ''.join(sorted(str(i))) == ''.join(sorted(str(5*i))) and\
''.join(sorted(str(i))) == ''.join(sorted(str(6*i))): ''.join(sorted(str(i))) == ''.join(sorted(str(6*i))):
break break
i = i + 1 i = i + 1
print('Project Euler, Problem 52') print('Project Euler, Problem 52')

View File

@ -18,7 +18,7 @@ from projecteuler import timing
@timing @timing
def p053(): def p053() -> None:
LIMIT = 1000000 LIMIT = 1000000
count = 0 count = 0