diff --git a/project_euler/problem_13/sol1.py b/project_euler/problem_13/sol1.py index e36065ec8..19b427337 100644 --- a/project_euler/problem_13/sol1.py +++ b/project_euler/problem_13/sol1.py @@ -1,30 +1,25 @@ """ +Problem 13: https://projecteuler.net/problem=13 + Problem Statement: Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. """ +import os -def solution(array): - """Returns the first ten digits of the sum of the array elements. +def solution(): + """ + Returns the first ten digits of the sum of the array elements + from the file num.txt - >>> import os - >>> sum = 0 - >>> array = [] - >>> with open(os.path.dirname(__file__) + "/num.txt","r") as f: - ... for line in f: - ... array.append(int(line)) - ... - >>> solution(array) + >>> solution() '5537376230' """ - return str(sum(array))[:10] + file_path = os.path.join(os.path.dirname(__file__), "num.txt") + with open(file_path, "r") as file_hand: + return str(sum([int(line) for line in file_hand]))[:10] if __name__ == "__main__": - n = int(input().strip()) - - array = [] - for i in range(n): - array.append(int(input().strip())) - print(solution(array)) + print(solution()) diff --git a/project_euler/problem_17/sol1.py b/project_euler/problem_17/sol1.py index d585d81a0..d4db1beb5 100644 --- a/project_euler/problem_17/sol1.py +++ b/project_euler/problem_17/sol1.py @@ -1,6 +1,6 @@ """ Number letter counts -Problem 17 +Problem 17: https://projecteuler.net/problem=17 If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. @@ -16,7 +16,7 @@ usage. """ -def solution(n): +def solution(n: int = 1000) -> int: """Returns the number of letters used to write all numbers from 1 to n. where n is lower or equals to 1000. >>> solution(1000) diff --git a/project_euler/problem_21/sol1.py b/project_euler/problem_21/sol1.py index f01c9d0da..3fac79156 100644 --- a/project_euler/problem_21/sol1.py +++ b/project_euler/problem_21/sol1.py @@ -1,5 +1,3 @@ -from math import sqrt - """ Amicable Numbers Problem 21 @@ -15,9 +13,10 @@ and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and Evaluate the sum of all the amicable numbers under 10000. """ +from math import sqrt -def sum_of_divisors(n): +def sum_of_divisors(n: int) -> int: total = 0 for i in range(1, int(sqrt(n) + 1)): if n % i == 0 and i != sqrt(n): @@ -27,7 +26,7 @@ def sum_of_divisors(n): return total - n -def solution(n): +def solution(n: int = 10000) -> int: """Returns the sum of all the amicable numbers under n. >>> solution(10000) diff --git a/project_euler/problem_31/sol1.py b/project_euler/problem_31/sol1.py index 09b60cdae..ba40cf383 100644 --- a/project_euler/problem_31/sol1.py +++ b/project_euler/problem_31/sol1.py @@ -1,6 +1,7 @@ """ Coin sums -Problem 31 +Problem 31: https://projecteuler.net/problem=31 + In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: @@ -12,39 +13,39 @@ How many different ways can £2 be made using any number of coins? """ -def one_pence(): +def one_pence() -> int: return 1 -def two_pence(x): +def two_pence(x: int) -> int: return 0 if x < 0 else two_pence(x - 2) + one_pence() -def five_pence(x): +def five_pence(x: int) -> int: return 0 if x < 0 else five_pence(x - 5) + two_pence(x) -def ten_pence(x): +def ten_pence(x: int) -> int: return 0 if x < 0 else ten_pence(x - 10) + five_pence(x) -def twenty_pence(x): +def twenty_pence(x: int) -> int: return 0 if x < 0 else twenty_pence(x - 20) + ten_pence(x) -def fifty_pence(x): +def fifty_pence(x: int) -> int: return 0 if x < 0 else fifty_pence(x - 50) + twenty_pence(x) -def one_pound(x): +def one_pound(x: int) -> int: return 0 if x < 0 else one_pound(x - 100) + fifty_pence(x) -def two_pound(x): +def two_pound(x: int) -> int: return 0 if x < 0 else two_pound(x - 200) + one_pound(x) -def solution(n): +def solution(n: int = 200) -> int: """Returns the number of different ways can n pence be made using any number of coins? @@ -61,4 +62,4 @@ def solution(n): if __name__ == "__main__": - print(solution(int(str(input()).strip()))) + print(solution(int(input().strip()))) diff --git a/project_euler/problem_31/sol2.py b/project_euler/problem_31/sol2.py index b390b5b1e..f9e4dc384 100644 --- a/project_euler/problem_31/sol2.py +++ b/project_euler/problem_31/sol2.py @@ -1,4 +1,7 @@ -"""Coin sums +""" +Problem 31: https://projecteuler.net/problem=31 + +Coin sums In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: @@ -29,7 +32,7 @@ Example: """ -def solution(pence: int) -> int: +def solution(pence: int = 200) -> int: """Returns the number of different ways to make X pence using any number of coins. The solution is based on dynamic programming paradigm in a bottom-up fashion. diff --git a/project_euler/problem_33/sol1.py b/project_euler/problem_33/sol1.py index 73a49023a..ba6e553d8 100644 --- a/project_euler/problem_33/sol1.py +++ b/project_euler/problem_33/sol1.py @@ -1,5 +1,5 @@ """ -Problem: +Problem 33: https://projecteuler.net/problem=33 The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe @@ -14,27 +14,30 @@ and denominator. If the product of these four fractions is given in its lowest common terms, find the value of the denominator. """ +from fractions import Fraction +from typing import List -def isDigitCancelling(num, den): +def is_digit_cancelling(num: int, den: int) -> bool: if num != den: if num % 10 == den // 10: if (num // 10) / (den % 10) == num / den: return True + return False -def solve(digit_len: int) -> str: +def fraction_list(digit_len: int) -> List[str]: """ - >>> solve(2) - '16/64 , 19/95 , 26/65 , 49/98' - >>> solve(3) - '16/64 , 19/95 , 26/65 , 49/98' - >>> solve(4) - '16/64 , 19/95 , 26/65 , 49/98' - >>> solve(0) - '' - >>> solve(5) - '16/64 , 19/95 , 26/65 , 49/98' + >>> fraction_list(2) + ['16/64', '19/95', '26/65', '49/98'] + >>> fraction_list(3) + ['16/64', '19/95', '26/65', '49/98'] + >>> fraction_list(4) + ['16/64', '19/95', '26/65', '49/98'] + >>> fraction_list(0) + [] + >>> fraction_list(5) + ['16/64', '19/95', '26/65', '49/98'] """ solutions = [] den = 11 @@ -42,14 +45,24 @@ def solve(digit_len: int) -> str: for num in range(den, last_digit): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): - if isDigitCancelling(num, den): + if is_digit_cancelling(num, den): solutions.append(f"{num}/{den}") den += 1 num += 1 den = 10 - solutions = " , ".join(solutions) return solutions +def solution(n: int = 2) -> int: + """ + Return the solution to the problem + """ + result = 1.0 + for fraction in fraction_list(n): + frac = Fraction(fraction) + result *= frac.denominator / frac.numerator + return int(result) + + if __name__ == "__main__": - print(solve(2)) + print(solution()) diff --git a/project_euler/problem_43/sol1.py b/project_euler/problem_43/sol1.py index 2fc429f9f..1febe4a4d 100644 --- a/project_euler/problem_43/sol1.py +++ b/project_euler/problem_43/sol1.py @@ -1,4 +1,6 @@ """ +Problem 43: https://projecteuler.net/problem=43 + The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. @@ -38,11 +40,11 @@ def is_substring_divisible(num: tuple) -> bool: return True -def compute_sum(n: int = 10) -> int: +def solution(n: int = 10) -> int: """ Returns the sum of all pandigital numbers which pass the divisiility tests. - >>> compute_sum(10) + >>> solution(10) 16695334890 """ list_nums = [ @@ -55,4 +57,4 @@ def compute_sum(n: int = 10) -> int: if __name__ == "__main__": - print(f"{compute_sum(10) = }") + print(f"{solution() = }") diff --git a/project_euler/problem_44/sol1.py b/project_euler/problem_44/sol1.py index 536720b39..d3ae6476d 100644 --- a/project_euler/problem_44/sol1.py +++ b/project_euler/problem_44/sol1.py @@ -1,4 +1,6 @@ """ +Problem 44: https://projecteuler.net/problem=44 + Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are: 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... @@ -24,11 +26,11 @@ def is_pentagonal(n: int) -> bool: return ((1 + root) / 6) % 1 == 0 -def compute_num(limit: int = 5000) -> int: +def solution(limit: int = 5000) -> int: """ Returns the minimum difference of two pentagonal numbers P1 and P2 such that P1 + P2 is pentagonal and P2 - P1 is pentagonal. - >>> compute_num(5000) + >>> solution(5000) 5482660 """ pentagonal_nums = [(i * (3 * i - 1)) // 2 for i in range(1, limit)] @@ -42,4 +44,4 @@ def compute_num(limit: int = 5000) -> int: if __name__ == "__main__": - print(f"{compute_num() = }") + print(f"{solution() = }") diff --git a/project_euler/problem_46/sol1.py b/project_euler/problem_46/sol1.py index e94e9247d..3fdf56755 100644 --- a/project_euler/problem_46/sol1.py +++ b/project_euler/problem_46/sol1.py @@ -1,4 +1,6 @@ """ +Problem 46: https://projecteuler.net/problem=46 + It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. @@ -84,5 +86,10 @@ def compute_nums(n: int) -> list[int]: return list_nums +def solution() -> int: + """Return the solution to the problem""" + return compute_nums(1)[0] + + if __name__ == "__main__": - print(f"{compute_nums(1) = }") + print(f"{solution() = }") diff --git a/project_euler/problem_551/sol1.py b/project_euler/problem_551/sol1.py index 817474b35..71956691a 100644 --- a/project_euler/problem_551/sol1.py +++ b/project_euler/problem_551/sol1.py @@ -167,7 +167,7 @@ def add(digits, k, addend): digits.append(digit) -def solution(n): +def solution(n: int = 10 ** 15) -> int: """ returns n-th term of sequence @@ -197,4 +197,4 @@ def solution(n): if __name__ == "__main__": - print(solution(10 ** 15)) + print(f"{solution() = }") diff --git a/project_euler/problem_63/sol1.py b/project_euler/problem_63/sol1.py index e429db07b..f6a8d3240 100644 --- a/project_euler/problem_63/sol1.py +++ b/project_euler/problem_63/sol1.py @@ -11,16 +11,16 @@ Using these conclusions, we will calculate the result. """ -def compute_nums(max_base: int = 10, max_power: int = 22) -> int: +def solution(max_base: int = 10, max_power: int = 22) -> int: """ Returns the count of all n-digit numbers which are nth power - >>> compute_nums(10, 22) + >>> solution(10, 22) 49 - >>> compute_nums(0, 0) + >>> solution(0, 0) 0 - >>> compute_nums(1, 1) + >>> solution(1, 1) 0 - >>> compute_nums(-1, -1) + >>> solution(-1, -1) 0 """ bases = range(1, max_base) @@ -31,4 +31,4 @@ def compute_nums(max_base: int = 10, max_power: int = 22) -> int: if __name__ == "__main__": - print(f"{compute_nums(10, 22) = }") + print(f"{solution(10, 22) = }")