def __add__(self,x): nn,nd = self.n*x.d + self.d*x.n, self.d*x.d gc = num.gcd(nn,nd) return Frac(nn/gc,nd/gc)
def __mul__(self,x): nn,nd = self.n*x.n, self.d*x.d gc = num.gcd(abs(nn),abs(nd)) return Frac(nn/gc,nd/gc)
def __div__(self,x): if x.n==0: raise 'divby0' nn,nd = self.n*x.d, self.d*x.n gc = num.gcd(abs(nn),abs(nd)) return Frac(nn/gc,nd/gc)
import time import math from num import gcd st = time.time() l = 1500000 d = [0]*l o = int(math.sqrt(l)) for i in range(1,o,2): for j in range(2,o,2): if gcd(i,j)==1: s = abs(i*i-j*j) + 2*i*j + i*i + j*j for k in range(s, l, s): d[k] += 1 print d.count(1) #print 'time:', time.time()-st