-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path73.py
67 lines (54 loc) · 1.76 KB
/
73.py
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 Sébastien Diemer <[email protected]>
"""
"""
from __future__ import division
from operator import mul
from fractions import gcd
def memo(f):
def memoized_f(*args):
if memoized_f._memo.has_key(args): return memoized_f._memo[args]
else:
result = f(*args)
memoized_f._memo[args] = result
return result
memoized_f._memo = {}
return memoized_f
@memo
def prime_factors(n):
i = 2
while i*i <= n:
if not n%i:
d = prime_factors(n/i).copy()
d[i] = d.get(i, 0) + 1
return d
i += 1
return {n:1}
assert prime_factors(5) == {5:1}
assert prime_factors(2) == {2:1}
assert prime_factors(3) == {3:1}
assert prime_factors(3) == {3:1}
assert prime_factors(8) == {2:3}
assert prime_factors(18) == {2:1, 3:2}
def is_divisible_by_any(n, factors):
return not bool(reduce(mul, [n%f for f in factors]))
assert is_divisible_by_any(8, [2, 3]) == True
def reduced_fractions(d_max=12000):
return [(n, d) for d in xrange(2, d_max+1)
for n in xrange(d//3+1, d//2+1)
if not is_divisible_by_any(n, prime_factors(d).keys())]
def sort_fractions(fractions):
return sorted(fractions, key=lambda t: t[0]/t[1])
def count_fractions_between(fractions):
sorted_frac = sort_fractions(fractions)
if sorted_frac[0] == (1, 3): sorted_frac = sorted_frac[1:]
if sorted_frac[-1] == (1, 2): sorted_frac = sorted_frac[:-1]
return len(sorted_frac)
assert count_fractions_between(reduced_fractions(8)) == 3
def problem_73():
fractions = reduced_fractions()
return count_fractions_between(fractions)
print "La solution est ", problem_73()