for x in range(7): for y in range(7): if (x*x+y*y) % 7 == 1: print (x,y) # output: (0, 1) # output: (0, 6) # output: (1, 0) # output: (2, 2) # output: (2, 5) # output: (5, 2) # output: (5, 5) # output: (6, 0) def clockadd(P1,P2): x1,y1 = P1 x2,y2 = P2 x3 = x1*y2+y1*x2 y3 = y1*y2-x1*x2 return x3,y3 def F(p): # caveat: caller must ensure that p is prime class F: def __init__(self,x): self.int = x % p def __str__(self): return str(self.int) __repr__ = __str__ def __eq__(a,b): return a.int == b.int def __ne__(a,b): return a.int != b.int def __add__(a,b): return F(a.int + b.int) def __sub__(a,b): return F(a.int - b.int) def __mul__(a,b): return F(a.int * b.int) def __div__(a,b): # caveat: caller must ensure that b is nonzero return a*F(pow(b.int,p-2,p)) return F F7 = F(7) P1 = (F7(2),F7(5)) P2 = (F7(1),F7(0)) print clockadd(P1,P2) print clockadd(P2,P1) P3 = (F7(5),F7(5)) print clockadd(clockadd(P1,P2),P3) print clockadd(P1,clockadd(P2,P3)) p = 1000003 Fp = F(p) P = (Fp(1000),Fp(2)) P2 = clockadd(P,P) print P2 P3 = clockadd(P2,P) print P3 P4 = clockadd(P3,P) P5 = clockadd(P4,P) P6 = clockadd(P5,P) print P6 print clockadd(P3,P3)