def pell_solution4(D): '''Generates solutions to x^2-D*y^2 = +-4. D must be 1 (mod 4) and >= 5.''' # Calculate the fundamental solution with RHS=-4 by brute force. PQa would have been faster. xt, yt = dropwhile(lambda (x, _): not is_int(x), ((D * y * y - 4, y) for y in count(1))).next() yield xt, yt x0, y0, x1, y1 = 2, 0, xt, yt while True: x, y = xt * x1 + x0, xt * y1 + y0 yield x, y x0, y0, x1, y1 = x1, y1, x, y
def prog_sq(N): '''Return the set of progressive perfect squares <= N.''' result = set([]) for q in xrange(2, int((N / 2.) ** (2. / 3.)) + 1): factors, Q = factorize(q), q * q * q P, c_max = map(long, factors.keys()), min(int(Q ** 0.5), q) # print 'q', q, 'q^3', Q, factors, 'c_max', c_max for K in product(*(xrange(3 * k + 1) for k in factors.itervalues())): c = prod([p ** k for p, k in zip(P, K)]) if c <= c_max: # print '\t', c, Q / c a2 = c + Q / c if a2 <= N and is_int(a2 ** 0.5): # print 'Found', 'q', q, 'q^3', Q, 'c', c, 'd', Q / c, 'n', a2, ' =', int(a2 ** 0.5), '^ 2' d = (a2 - c) / q print 'Found', 'n', a2, ' =', int(a2 ** 0.5), '^ 2 =', 'q', q, 'd', d, 'r', c, 'd**0.5', d ** 0.5, '(d/2)**0.5', (d / 2.) ** 0.5 result.add(a2) return result