Solved 68 and rewrote 69 in Python.

This commit is contained in:
2019-07-18 22:52:08 -04:00
parent 68f23d2d09
commit 66e4593c4b
2 changed files with 88 additions and 59 deletions

View File

@@ -1,62 +1,23 @@
from lib_prime import primes
from lib_misc import gcd
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 relative_primes_count(n):
return len([i for i in range(1, n) if gcd(n, i) == 1])
def get_phi(n):
phi = n
for p in prime_factors_unique(n):
phi *= (1 - 1 / p)
return int(phi)
return n / relative_primes_count(n)
print(get_phi(210412312))
def euler_069():
s = 1
for p in primes(1000):
s *= p
if s > 1000000:
return s // p
if __name__ == "__main__":
print("e069.py: " + str(euler_069()))
assert(euler_069() == 510510)