Add type hints in problems 41-50
This commit is contained in:
parent
3a4f85db04
commit
44fd50f067
@ -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
|
||||
|
@ -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(','))
|
||||
|
@ -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__':
|
||||
|
@ -13,7 +13,7 @@ from projecteuler import is_pentagonal, timing
|
||||
|
||||
|
||||
@timing
|
||||
def p044():
|
||||
def p044() -> None:
|
||||
found = 0
|
||||
n = 2
|
||||
|
||||
|
@ -14,7 +14,7 @@ from projecteuler import is_pentagonal, timing
|
||||
|
||||
|
||||
@timing
|
||||
def p045():
|
||||
def p045() -> None:
|
||||
found = 0
|
||||
i = 143
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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__':
|
||||
|
@ -12,7 +12,7 @@ from projecteuler import sieve, timing
|
||||
|
||||
|
||||
@timing
|
||||
def p049():
|
||||
def p049() -> None:
|
||||
N = 10000
|
||||
|
||||
primes = sieve(N)
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user