-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path70.py
134 lines (110 loc) · 2.94 KB
/
70.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 Sébastien Diemer <[email protected]>
"""
Problem 70
"""
def is_permutation(m, n):
s1, s2 = str(m), str(n)
result = all((s1.count(c) == s2.count(c) for c in s1))
if result: print m, n
return result
assert is_permutation(87109, 79180)
assert not is_permutation(12345, 12346)
assert not is_permutation(66088, 66089)
assert is_permutation(66098, 66089)
assert not is_permutation(40697, 40696)
assert not is_permutation(84499, 84498)
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
PRIMES = [2, 3]
@memo
def euler_function2(n):
if n == 1: return 1
if n == 2: return 1
if n == 3: return 2
for i in PRIMES:
exp, j = 0, n
while j and (not j%i):
exp += 1
j = j/i
if exp:
return (i**exp - i**(exp-1))*euler_function2(n/(i**exp))
PRIMES.append(n)
return n - 1
#print problem70()
import numpy as np
def build_primes(N=10**7):
primes = np.ones(N)
primes[0] = 0
primes[1] = 0
for i in xrange(2, int(N**0.5 + 1)):
if primes[i]:
j = 2
while i*j < N:
primes[i*j] = 0
j += 1
return primes
PRIMES = build_primes(10000000)
print "Done with PRIMES"
def is_prime(n):
return PRIMES[n] == 1
assert is_prime(13)
assert is_prime(17)
assert not is_prime(21)
def build_prime_factors():
N = len(PRIMES)
factors = [[] for _ in xrange(N)]
for i, is_prime in enumerate(PRIMES):
if is_prime:
j = 1
while i*j < N:
factors[i*j].append(i)
j += 1
return factors
FACTORS = build_prime_factors()
print "Done with factors"
def prime_factors(n):
return FACTORS[n]
assert prime_factors(2) == [2]
assert prime_factors(4) == [2]
assert prime_factors(6) == [2, 3]
assert prime_factors(90) == [2, 3, 5]
def euler_function2(n):
if n == 1: return 1
factors = prime_factors(n)
return (n*reduce(mul, [f-1 for f in factors]))/reduce(mul, factors)
assert euler_function2(2) == 1
assert euler_function2(3) == 2
assert euler_function2(91) == 72
def problem70():
mini, value = 10, -1
for n in xrange(2, 10000000):
euler = euler_function2(n)
if is_permutation(n, euler):
if float(n)/euler < mini:
mini, value = float(n)/euler, n
return value
print problem70()