Skip to content

Commit 2e1655e

Browse files
committed
PE70: Phi in NlogN, interview
1 parent 7681e3d commit 2e1655e

File tree

1 file changed

+32
-0
lines changed
  • MyOnlineSubmissions/ProjectEuler

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Interestingly, φ(87109)=79180, and it can be seen that 87109 is a permutation of 79180.
2+
# Find the value of n, 1 < n < 10^7, for which φ(n) is a permutation of n and the ratio n/φ(n) produces a minimum.
3+
N = 10**7
4+
rat = N + 1
5+
ans = -1
6+
7+
def same(x, y):
8+
return sorted(str(x)) == sorted(str(y))
9+
10+
def euler_phi(MAX=N+1):
11+
phi = [0 for _ in range(MAX+1)]
12+
phi[1] = 1
13+
for i in range(2, MAX+1):
14+
if not phi[i]:
15+
phi[i] = i-1
16+
for j in range((i<<1), MAX+1, i):
17+
if not phi[j]:
18+
phi[j] = j
19+
phi[j] = (phi[j]//i) * (i-1)
20+
return phi
21+
22+
phi = euler_phi()
23+
24+
for n in range(2, N + 1):
25+
p = phi[n]
26+
if same(p, n):
27+
print(n, p)
28+
if rat > n/p:
29+
rat = n/p
30+
ans = n
31+
32+
print(ans)

0 commit comments

Comments
 (0)