Ejemplo n.º 1
0
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.
'''


def EulerProduct(n,factors):
	# http://en.wikipedia.org/wiki/Euler's_totient_function
	product = n
	for i in set(factors):
		product *= (1-1/i)
	return product

n_limit = 100
# primes = sieve_for_primes_to(n_limit)
# print(len(primes))
print(list(enumerate(sieve_for_primes_to(n_limit))))
print([i*2+1 for i, v in enumerate(sieve_for_primes_to(n_limit)) if v and i>0])
# print(len([2] + [i*2+1 for i, v in enumerate(sieve_for_primes_to(10000000)) if v and i>0]))
# print(primes)
print("done")

# primes = set()
# min_ratio = float("Inf")
# min_n = 0
# min_phi = 0
# for n in range(2,n_limit+1):
# 	factors = prime_factors(n)
# 	if len(factors)==2:
# 		phi = int(EulerProduct(n,prime_factors(n)))
# 		if sorted(str(n)) == sorted(str(phi)):
# 			ratio = n/phi
Ejemplo n.º 2
0
    factors = []
    d = 2
    while n > 1:
        while n % d == 0:
            factors.append(d)
            n /= d
        d = d + 1
        if d*d > n:
            if n > 1: factors.append(n)
            break
    return factors

total = 0
limit = 100000

primes = {2} | {i*2+1 for i, v in enumerate(sieve_for_primes_to(limit)) if v and i>0}

start = time.time()
for n in range(2,limit+1):
	if n in primes: 
		total += n-1
	else:
		factors = {i for i in primes if n%i==0 and i<=n*0.5}
		
		phi = int(EulerProduct(n,factors))
		total += phi
	if n%10000==0:
		print(n,time.time()-start)
		start = time.time()