Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix code style for multiple problems
  • Loading branch information
dhruvmanila committed Oct 9, 2020
commit a02cd8193f431ca13657a58675b374317295844d
23 changes: 12 additions & 11 deletions project_euler/problem_31/sol1.py
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -12,39 +13,39 @@
"""


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?

Expand All @@ -61,4 +62,4 @@ def solution(n):


if __name__ == "__main__":
print(solution(int(str(input()).strip())))
print(solution(int(input().strip())))
7 changes: 5 additions & 2 deletions project_euler/problem_31/sol2.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -29,7 +32,7 @@
"""


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.

Expand Down
45 changes: 29 additions & 16 deletions project_euler/problem_33/sol1.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,42 +14,55 @@
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
last_digit = int("1" + "0" * digit_len)
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())
8 changes: 5 additions & 3 deletions project_euler/problem_43/sol1.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 = [
Expand All @@ -55,4 +57,4 @@ def compute_sum(n: int = 10) -> int:


if __name__ == "__main__":
print(f"{compute_sum(10) = }")
print(f"{solution() = }")
8 changes: 5 additions & 3 deletions project_euler/problem_44/sol1.py
Original file line number Diff line number Diff line change
@@ -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, ...
Expand All @@ -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)]
Expand All @@ -42,4 +44,4 @@ def compute_num(limit: int = 5000) -> int:


if __name__ == "__main__":
print(f"{compute_num() = }")
print(f"{solution() = }")
9 changes: 8 additions & 1 deletion project_euler/problem_46/sol1.py
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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() = }")
4 changes: 2 additions & 2 deletions project_euler/problem_551/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -197,4 +197,4 @@ def solution(n):


if __name__ == "__main__":
print(solution(10 ** 15))
print(f"{solution() = }")
12 changes: 6 additions & 6 deletions project_euler/problem_63/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"""


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)
Expand All @@ -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) = }")