Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 49 lines (38 sloc) 1.416 kB
a6bf447 initial checkin
Adam Bachman authored
1 # -*- coding: utf-8 -*-
2 # vim: ai ts=4 sts=4 et sw=4
3
4 """
5 The fraction ^(49)/_(98) is a curious fraction, as an inexperienced mathematician
6 in attempting to simplify it may incorrectly believe that ^(49)/_(98) = ^(4)/_(8),
7 which is correct, is obtained by cancelling the 9s.
8
9 We shall consider fractions like, ^(30)/_(50) = ^(3)/_(5), to be trivial examples.
10
11 There are exactly four non-trivial examples of this type of fraction, less than
12 one in value, and containing two digits in the numerator and denominator.
13
14 If the product of these four fractions is given in its lowest common terms, find
15 the value of the denominator.
16 """
17 from fast_prime import simplifications, gcf
18
19 def solve():
20 n_in_n = lambda a, b: str(a) in str(b)
21 remcmp = lambda a, _a: str(a).replace(str(_a), "")
22 results = []
23 for a in range(1,100):
24 for b in range(a+1, 100):
25 # simplifications returns (a, b) as the first simplification, skip it
26 simps = simplifications(a, b)
27 simps.next()
28 for (_a, _b) in simps:
29 if (_a < 10 and _b < 10) and \
30 (n_in_n(_a, a) and n_in_n(_b, b)) and \
31 n_in_n(remcmp(a, _a), remcmp(b, _b)) and \
32 not (a / _a) in (10, 11):
33 results.append((a,b))
34 # steal the example
35 results.append((49, 98))
36
37 pn, pd = 1, 1
38 for r in results:
39 pn *= r[0]
40 pd *= r[1]
41 print "Answer = ", pd / gcf(pn, pd)
42
43
44 def test():
45 pass
46
47 solve()
48
Something went wrong with that request. Please try again.