-
Notifications
You must be signed in to change notification settings - Fork 0
/
70.py
53 lines (43 loc) · 1.28 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
from functools import cache
from math import ceil, sqrt
from sympy import totient, sieve
from tqdm import trange
from tqdm.std import tqdm
def is_perm(a, b):
return sorted(str(a)) == sorted(str(b))
# This is Jonas's TRASH solution (actually probably just a little slower and way simpler)
# print(argmin([n/phin for n in trange(1, 10_000_000) if is_perm(n, (phin := totient(n)))]) + 1)
top = 10 ** 7
PRIMES = list(sieve.primerange(0, ceil(sqrt(top))))
@cache
def factorsOf(n):
factors = []
halfN = ceil(sqrt(n))
if n in PRIMES:
return [n]
for p in PRIMES:
if p > halfN:
break
div, mod = divmod(n, p)
if mod == 0:
factors.append(p)
prevDiv = div
div, mod = divmod(div, p)
while mod == 0:
prevDiv = div
div, mod = divmod(div, p)
factors += factorsOf(prevDiv)
return set(factors)
def nOnPhiN(n):
# p = factorint(n).keys()
p = factorsOf(n)
total = 1
for fact in p:
total *= (fact - 1) / fact
return 1 / total
possible = sorted([(nOnPhiN(n), n) for n in trange(2, top)], key=lambda a: (a[0], top - a[1]))
for pair in tqdm(possible):
ratio, n = pair
if is_perm(n, totient(n)):
print(n)
break