-
Notifications
You must be signed in to change notification settings - Fork 0
/
72.py
46 lines (39 loc) · 1.2 KB
/
72.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 Sébastien Diemer <[email protected]>
"""
L'idée de cette solution repose sur la fonction Totient d'euler, qui compte
le nombre d'entiers inférieurs à n et premiers avec n.
Cette fonction vaut n-1 pour n premier, est multiplicative entre deux nombres
premiers entre eux.
"""
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}
def get_prime_factors(n):
return [prime_factors(n) for n in xrange(2, n+1)]
from operator import mul
def euler_product(factors):
return reduce(mul, [f**i - f**(i-1) for f, i in factors.items()])
def count_fractions(d_max=8):
factors = get_prime_factors(d_max)
return sum([euler_product(f) for f in factors])
print count_fractions(1000000)