Solved 68 and rewrote 69 in Python.
parent
68f23d2d09
commit
66e4593c4b
|
@ -1,8 +1,76 @@
|
|||
from lib_misc import permutations
|
||||
|
||||
|
||||
def all_equal(xs):
|
||||
x_1 = xs[0]
|
||||
for x in xs[1:]:
|
||||
if x_1 != x:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_3_gon_ring(r):
|
||||
if not (r[2] > r[0] and r[1] > r[0]):
|
||||
return False
|
||||
line_034 = r[0] + r[3] + r[4]
|
||||
line_154 = r[1] + r[5] + r[4]
|
||||
line_253 = r[2] + r[5] + r[3]
|
||||
return all_equal([line_034, line_154, line_253])
|
||||
|
||||
|
||||
def is_5_gon_ring(r):
|
||||
if not (r[1] > r[0] and r[2] > r[0] and
|
||||
r[3] > r[0] and r[4] > r[0]):
|
||||
return False
|
||||
l_056 = r[0] + r[5] + r[6]
|
||||
l_167 = r[1] + r[6] + r[7]
|
||||
l_278 = r[2] + r[7] + r[8]
|
||||
l_389 = r[3] + r[8] + r[9]
|
||||
l_495 = r[4] + r[9] + r[5]
|
||||
return all_equal([l_056, l_167, l_278, l_389, l_495])
|
||||
|
||||
|
||||
def five_gon_ring_to_group_presentation(r):
|
||||
xs = [r[0], r[5], r[6],
|
||||
r[1], r[6], r[7],
|
||||
r[2], r[7], r[8],
|
||||
r[3], r[8], r[9],
|
||||
r[4], r[9], r[5]]
|
||||
xs = "".join(map(str, xs))
|
||||
return xs
|
||||
|
||||
|
||||
def euler_068():
|
||||
# XXX: continue here
|
||||
return 0
|
||||
"""
|
||||
This one was really fun. The hardest part was to get the
|
||||
check for the representation right. This is how my five
|
||||
gon ring is indexed:
|
||||
|
||||
# 0 1 #
|
||||
# 5 #
|
||||
# 9 6 #
|
||||
# 4 #
|
||||
# 8 7 2 #
|
||||
# #
|
||||
# 3 #
|
||||
|
||||
There is probably and automatic indexing scheme that would
|
||||
make the check and presentation function easier to write, but not
|
||||
necessarily easier to read. Also generating all permutations creates
|
||||
a lot of over head. For example, the lowest number cannot be higher
|
||||
than six so that removes about 40% of the permutations right away and
|
||||
there are probably more heuristics.
|
||||
"""
|
||||
rs = []
|
||||
for p in permutations(list(range(1, 11))):
|
||||
if is_5_gon_ring(p):
|
||||
r = five_gon_ring_to_group_presentation(p)
|
||||
if len(r) == 16:
|
||||
print(r)
|
||||
rs.append(int(r))
|
||||
return max(rs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("e068.py: " + str(euler_068()))
|
||||
assert(euler_068() == 0)
|
||||
assert(euler_068() == 6531031914842725)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue