def max_square(anagram_pair): s1, s2 = anagram_pair letters = sorted(list(set(s1))) l = len(letters) max_val = 0 if l <= 10: for tup in permutations([str(i) for i in range(10)], l): number_s1 = s1 number_s2 = s2 for i in xrange(l): number_s1 = number_s1.replace(letters[i], tup[i]) number_s2 = number_s2.replace(letters[i], tup[i]) if number_s1[0] != "0" and number_s2[0] != "0": if is_square(int(number_s1)) and is_square(int(number_s2)): max_val = max(max_val, int(number_s1), int(number_s2)) return max_val
def f(n): """ >>> sum(set(map(sum,f(120000)))) 30758397 >>> sum(map(sum,f(20000))) 679255 >>> map(sum, f(1000)) [784] """ d = defaultdict(set) for r in range(2, n): for q in range(1, min(r, n - r)): if is_square(r * r + r * q + q * q): for p in d[q] & d[r]: if p < q and p + q + r <= n: yield p, q, r assert p < q < r d[q].add(r) d[r].add(q)
def f(n): """ c^2d, cbd, b^2d (c < b) cb^3d^2 + c^2b=m^2 求出所有x = m^2 = cb^3d^2 + c^2b < n (c, b, d, m 为整数) >>> sum(set(f(10 ** 5))) 124657 >>> sum(set(f(10 ** 12))) 878454337159 """ for b in count(2): b3 = b ** 3 if b3 > n: break for c in range(1, b): for d in count(1): x = c * b3 * d * d + c * c * d if x > n: break if is_square(x): yield x