From b22d4f32894692efa88ce3ad452f0997a799101c Mon Sep 17 00:00:00 2001 From: pingings <56961474+pingings@users.noreply.github.com> Date: Thu, 24 Oct 2019 16:43:04 +0100 Subject: [PATCH 1/5] Create sol1.py --- project_euler/problem_33/sol1.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 project_euler/problem_33/sol1.py diff --git a/project_euler/problem_33/sol1.py b/project_euler/problem_33/sol1.py new file mode 100644 index 000000000000..747f6faf3933 --- /dev/null +++ b/project_euler/problem_33/sol1.py @@ -0,0 +1,23 @@ +def isDigitCancelling(num,den): + if num != den: + if num % 10 == den // 10: + if (num // 10) / (den % 10) == num/den: + return True + +def solve(): + solutions = [] + den = 11 + for num in range(11,99): + while den <= 99: + if (num != den) and (num % 10 == den // 10) and (den%10 != 0): + if isDigitCancelling(num,den): + solutions.append("{}/{}".format(num,den)) + den += 1 + num += 1 + den = 10 + return solutions + + +if __name__ == "__main__": + print(*solve(),sep=" , ") + From 3e3f178f23f557abb530e5e1cc8b846ce9a0712a Mon Sep 17 00:00:00 2001 From: pingings <56961474+pingings@users.noreply.github.com> Date: Thu, 24 Oct 2019 16:43:19 +0100 Subject: [PATCH 2/5] Create __init__.py --- project_euler/problem_33/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 project_euler/problem_33/__init__.py diff --git a/project_euler/problem_33/__init__.py b/project_euler/problem_33/__init__.py new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/project_euler/problem_33/__init__.py @@ -0,0 +1 @@ + From a8a3c8b2ad3282810dede60ed578d5b7adfc0444 Mon Sep 17 00:00:00 2001 From: pingings <56961474+pingings@users.noreply.github.com> Date: Thu, 24 Oct 2019 16:44:00 +0100 Subject: [PATCH 3/5] Update sol1.py --- project_euler/problem_33/sol1.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/project_euler/problem_33/sol1.py b/project_euler/problem_33/sol1.py index 747f6faf3933..cfb19f74b76f 100644 --- a/project_euler/problem_33/sol1.py +++ b/project_euler/problem_33/sol1.py @@ -1,3 +1,23 @@ +""" + +Problem: + +The fraction 49/98 is a curious fraction, as an inexperienced +mathematician in attempting to simplify it may incorrectly believe +that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s. + +We shall consider fractions like, 30/50 = 3/5, to be trivial examples. + +There are exactly four non-trivial examples of this type of fraction, +less than one in value, and containing two digits in the numerator +and denominator. + +If the product of these four fractions is given in its lowest common +terms, find the value of the denominator. + +""" + + def isDigitCancelling(num,den): if num != den: if num % 10 == den // 10: From 7903c926d604a4d35551f09a967441f383671648 Mon Sep 17 00:00:00 2001 From: pingings <56961474+pingings@users.noreply.github.com> Date: Thu, 24 Oct 2019 16:47:24 +0100 Subject: [PATCH 4/5] corrected range --- project_euler/problem_33/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_33/sol1.py b/project_euler/problem_33/sol1.py index cfb19f74b76f..98ff65d79aa3 100644 --- a/project_euler/problem_33/sol1.py +++ b/project_euler/problem_33/sol1.py @@ -27,7 +27,7 @@ def isDigitCancelling(num,den): def solve(): solutions = [] den = 11 - for num in range(11,99): + for num in range(11,100): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den%10 != 0): if isDigitCancelling(num,den): From afacb8140ef32989bd395549935e281ab0c18015 Mon Sep 17 00:00:00 2001 From: vinayak Date: Thu, 31 Oct 2019 17:36:55 +0530 Subject: [PATCH 5/5] Update sol1.py --- project_euler/problem_33/sol1.py | 48 ++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/project_euler/problem_33/sol1.py b/project_euler/problem_33/sol1.py index 98ff65d79aa3..0992c96935f5 100644 --- a/project_euler/problem_33/sol1.py +++ b/project_euler/problem_33/sol1.py @@ -1,43 +1,55 @@ """ - Problem: -The fraction 49/98 is a curious fraction, as an inexperienced -mathematician in attempting to simplify it may incorrectly believe +The fraction 49/98 is a curious fraction, as an inexperienced +mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s. We shall consider fractions like, 30/50 = 3/5, to be trivial examples. -There are exactly four non-trivial examples of this type of fraction, -less than one in value, and containing two digits in the numerator +There are exactly four non-trivial examples of this type of fraction, +less than one in value, and containing two digits in the numerator and denominator. -If the product of these four fractions is given in its lowest common +If the product of these four fractions is given in its lowest common terms, find the value of the denominator. - """ -def isDigitCancelling(num,den): +def isDigitCancelling(num, den): if num != den: if num % 10 == den // 10: - if (num // 10) / (den % 10) == num/den: + if (num // 10) / (den % 10) == num / den: return True -def solve(): + +def solve(digit_len: int) -> 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' + """ solutions = [] - den = 11 - for num in range(11,100): + 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): - solutions.append("{}/{}".format(num,den)) + if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): + if isDigitCancelling(num, den): + solutions.append("{}/{}".format(num, den)) den += 1 num += 1 - den = 10 + den = 10 + solutions = " , ".join(solutions) return solutions if __name__ == "__main__": - print(*solve(),sep=" , ") - + print(solve(2))