Add type hints in problems 1-10
This commit is contained in:
parent
51ed3838ab
commit
2d3b9eb45b
@ -8,7 +8,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p001():
|
def p001() -> None:
|
||||||
sum_ = 0
|
sum_ = 0
|
||||||
|
|
||||||
# Simple brute-force approach: try every number between 3 and 999,
|
# Simple brute-force approach: try every number between 3 and 999,
|
||||||
|
@ -10,7 +10,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p002():
|
def p002() -> None:
|
||||||
N = 4000000
|
N = 4000000
|
||||||
|
|
||||||
fib1 = 1
|
fib1 = 1
|
||||||
|
@ -10,7 +10,7 @@ from projecteuler import is_prime, timing
|
|||||||
# Recursive approach: if num is prime, return num, otherwise
|
# Recursive approach: if num is prime, return num, otherwise
|
||||||
# recursively look for the largest prime factor of num divided
|
# recursively look for the largest prime factor of num divided
|
||||||
# by its prime factors until only the largest remains.
|
# by its prime factors until only the largest remains.
|
||||||
def max_prime_factor(num):
|
def max_prime_factor(num: int) -> int:
|
||||||
# Use function defined in projecteuler.py to check if a number is prime.
|
# Use function defined in projecteuler.py to check if a number is prime.
|
||||||
if is_prime(num):
|
if is_prime(num):
|
||||||
return num
|
return num
|
||||||
@ -34,7 +34,7 @@ def max_prime_factor(num):
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p003():
|
def p003() -> None:
|
||||||
res = max_prime_factor(600851475143)
|
res = max_prime_factor(600851475143)
|
||||||
|
|
||||||
print('Project Euler, Problem 3')
|
print('Project Euler, Problem 3')
|
||||||
|
@ -8,7 +8,7 @@ from projecteuler import is_palindrome, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p004():
|
def p004() -> None:
|
||||||
max_ = 0
|
max_ = 0
|
||||||
|
|
||||||
# Using a brute-force approach: generate every product of 3-digit numbers
|
# Using a brute-force approach: generate every product of 3-digit numbers
|
||||||
|
@ -8,7 +8,7 @@ from projecteuler import lcmm, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p005():
|
def p005() -> None:
|
||||||
values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
||||||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
|
11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p006():
|
def p006() -> None:
|
||||||
sum_squares = 0
|
sum_squares = 0
|
||||||
square_sum = 0
|
square_sum = 0
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ from projecteuler import is_prime, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p007():
|
def p007() -> None:
|
||||||
count = 1
|
count = 1
|
||||||
n = 1
|
n = 1
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p008():
|
def p008() -> None:
|
||||||
number = '73167176531330624919225119674426574742355349194934' +\
|
number = '73167176531330624919225119674426574742355349194934' +\
|
||||||
'96983520312774506326239578318016984801869478851843' +\
|
'96983520312774506326239578318016984801869478851843' +\
|
||||||
'85861560789112949495459501737958331952853208805511' +\
|
'85861560789112949495459501737958331952853208805511' +\
|
||||||
@ -53,7 +53,7 @@ def p008():
|
|||||||
# Transform the string into a list of integers
|
# Transform the string into a list of integers
|
||||||
number = list(map(int, number))
|
number = list(map(int, number))
|
||||||
|
|
||||||
max_ = 0
|
_max = 0
|
||||||
|
|
||||||
# Calculate all the 13-digit products, and save the maximum
|
# Calculate all the 13-digit products, and save the maximum
|
||||||
for i in range(1000-13):
|
for i in range(1000-13):
|
||||||
@ -63,11 +63,10 @@ def p008():
|
|||||||
for j in curr:
|
for j in curr:
|
||||||
prod = prod * j
|
prod = prod * j
|
||||||
|
|
||||||
if prod > max_:
|
_max = max(_max, prod)
|
||||||
max_ = prod
|
|
||||||
|
|
||||||
print('Project Euler, Problem 8')
|
print('Project Euler, Problem 8')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {_max}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -15,7 +15,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p009():
|
def p009() -> None:
|
||||||
found = 0
|
found = 0
|
||||||
|
|
||||||
m = 2
|
m = 2
|
||||||
@ -34,6 +34,7 @@ def p009():
|
|||||||
|
|
||||||
if a + b + c == 1000:
|
if a + b + c == 1000:
|
||||||
found = 1
|
found = 1
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
i = 2
|
i = 2
|
||||||
@ -48,6 +49,7 @@ def p009():
|
|||||||
b = tmpb
|
b = tmpb
|
||||||
c = tmpc
|
c = tmpc
|
||||||
found = 1
|
found = 1
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
if tmpa + tmpb + tmpc > 1000:
|
if tmpa + tmpb + tmpc > 1000:
|
||||||
|
@ -8,7 +8,7 @@ from projecteuler import sieve, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p010():
|
def p010() -> None:
|
||||||
N = 2000000
|
N = 2000000
|
||||||
|
|
||||||
# Use the function in projecteuler.py implementing the
|
# Use the function in projecteuler.py implementing the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user