from operator import mul
import re
from __future__ import print, division
# Spacing changed to match language standard:
# https://www.python.org/dev/peps/pep-0008/
def hexit(fac):
c0, x = 1, 1
for p, e in fac.items():
c0 *= p**(e%6)
x *= p**(e//6) # Python 2 has / do C-like int div, while Python 3
# does more intuitive /-float, //-int divs
# https://www.python.org/dev/peps/pep-0238/
# You could also render it like this:
#q, r = divmod(e, 6)
#c0 *= p**r
#x *= p**q
return c0, x
def facform(n):
# The simplest possible trial division
fac = {}
for r in range(2, n+1):
e = 0
while n % r == 0:
print(n, r)
e += 1
n //= r
if e > 0:
fac[r] = e
return fac
def ffpow(ff, E):
out = {}
for p, e in ff.items():
out[p] = E*e
return out
def handle_common_factors(x, y):
# Order of arguments matters. This function directly mutates the arguments.
for p, e in x.keys():
if (e % 6) > 3:
more = 6 - (e%6)
x[p] += more
if p in y.keys():
y[p] += more
else:
y[p] = more
def handle_job(job):
print(job)
m = re.search('C([0-9]+)_([0-9]+)_([0-9]+)', job)
d = m.group(1)
x = int(m.group(2))
y = int(m.group(3))
N = x**y + y**x
ss = [r for r in results if '{}^{}'.format(x, y) in r][0]
factors = ss.split('=')[1].split('*')
for f in factors:
if f[0] != 'C':
if '^' in f:
p, e = [int(i) for i in f.split('^')]
pe = p**e
else:
pe = int(f)
N //= pe
print(N)
# start making the ggnfs input file
snfs = ['n: {}'.format(N)]
left = ffpow(facform(x), y)
right = ffpow(facform(y), x)
print(left, right)
# remove common factors
for p in left.keys():
if p in right:
print("FISH", p)
# Even if I hadn't moved this out to a function, you could do the swapping
# like this:
# for u, v in [(left, right), (right, left)]:
# for g in u.keys():
# ...
handle_common_factors(left, right)
handle_common_factors(right, left)
print("After handling common factors:", left, right)
c0, Y1 = hexit(left)
c6, Y0 = hexit(right)
tmplt = '{}: {}'
polys = [tmplt.format(var, eval(var)) for var in ('c6', 'c0', 'Y0', 'Y1')]
# This ^ is a funkier way to do it, nothing wrong with the original way.
# Arguably this is harder to read
initial_params = ["lpbr: 31", "lpba: 31", "mfbr: 62", "mfba: 62", "alambda: 2.6", "rlambda: 2.6", "alim: 134217727", "rlim: 134217727"]
snfs = snfs + ["skew: " + str(((c0+0.0)/(c6+0.0))**(1./6))] + polys + initial_params
with open(job+'.initial_snfs', 'w') as out:
out.write('\n'.join(snfs) + '\n')
# Best practice is to do file-io in `with` blocks:
with open('candidates.txt', 'r') as f:
lines = [l.strip() for l in f]
with open('../results2.txt', 'r') as g:
results = [l.strip() for l in g]
for job in lines:
handle_job(job)