-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem33.py
More file actions
84 lines (63 loc) · 3.1 KB
/
Copy pathProblem33.py
File metadata and controls
84 lines (63 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
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 reduce_fraction(num, denom):
from maths.misc import find_divisors
# Find divisors of num & denom
proper_divisors = True
div_n = find_divisors(num, proper_divisors)
div_d = find_divisors(denom, proper_divisors)
# Find intersection between divisors of numerator and denominator
intersection = list(div_n.intersection(div_d))
# Sort descending
intersection.sort()
intersection.reverse()
for i in intersection:
# Check that num and denom are still evenly divisible by i
# Need to check this as we're not only reducing by prime numbers
# We don't want to "over-reduce"
if num % i == 0 and denom % i == 0:
num = num / i
denom = denom / i
return num, denom
if __name__ == "__main__":
from maths.misc import get_digits
numerators = []
denominators = []
for i in xrange(10,100):
for j in xrange(i + 1,100):
# Get digits of i,j
digits_i, digits_j = get_digits(i), get_digits(j)
# Check for overlap
overlap = filter(set(digits_i).__contains__, digits_j)
if len(overlap) > 0:
# Pull first char from overlap
char_to_remove = overlap[0]
# Check for trivial case where digits to be removed are in the same power of 10s position
index_i = digits_i.index(char_to_remove)
index_j = digits_j.index(char_to_remove)
if index_i == index_j:
# Trivial case
continue
# Remove the char from i,j
digits_i.remove(char_to_remove)
digits_j.remove(char_to_remove)
num, denom = digits_i[0], digits_j[0]
if denom == 0:
# Can't divide by 0
continue
# Check if fractions are the same
if float(num) / denom == float(i) / j:
numerators.append(num)
denominators.append(denom)
# Lambda to multiply all elements in a list together
product_lambda = lambda x, y: x*y
# Find product of numerators, denominators
numerator = reduce(product_lambda, numerators)
denominator = reduce(product_lambda, denominators)
# Reduce numerator/denominator
numerator, denominator = reduce_fraction(numerator, denominator)
print('The reduced fraction is %d/%d'%(numerator, denominator))