63 lines
1.2 KiB
Python
63 lines
1.2 KiB
Python
|
|
|
|
def prime_factors(n):
|
|
f = []
|
|
while n % 2 == 0:
|
|
f.append(2)
|
|
n //= 2
|
|
d = 3
|
|
while d * d <= n:
|
|
while n % d == 0:
|
|
f.append(d)
|
|
n //= d
|
|
d += 2
|
|
if n != 1:
|
|
f.append(n)
|
|
return f
|
|
|
|
assert(prime_factors(8) == [2, 2, 2])
|
|
assert(prime_factors(2501232) == [2, 2, 2, 2, 3, 107, 487])
|
|
|
|
|
|
def prime_factors_unique(n):
|
|
f = []
|
|
if n % 2 == 0:
|
|
f.append(2)
|
|
while n % 2 == 0:
|
|
n //= 2
|
|
d = 3
|
|
while d * d <= n:
|
|
if n % d == 0:
|
|
f.append(d)
|
|
while n % d == 0:
|
|
n //= d
|
|
d += 2
|
|
if n != 1:
|
|
f.append(n)
|
|
return f
|
|
|
|
|
|
def prime_factors_count(n):
|
|
return len(prime_factors_unique(n))
|
|
|
|
|
|
print(prime_factors_count(9699690))
|
|
|
|
#factor_count_max = 0
|
|
#n_max = 0
|
|
#for n in range(1, 1000001):
|
|
# factor_count = prime_factors_count(n)
|
|
# if factor_count > factor_count_max:
|
|
# factor_count_max = factor_count
|
|
# n_max = n
|
|
#print("factor count: {} n: {}".format(factor_count_max, n_max))
|
|
|
|
|
|
def get_phi(n):
|
|
phi = n
|
|
for p in prime_factors_unique(n):
|
|
phi *= (1 - 1 / p)
|
|
return int(phi)
|
|
|
|
print(get_phi(210412312))
|