for n in range(0,20): print n,n%7 # output: 0 0 # output: 1 1 # output: 2 2 # output: 3 3 # output: 4 4 # output: 5 5 # output: 6 6 # output: 7 0 # output: 8 1 # output: 9 2 # output: 10 3 # output: 11 4 # output: 12 5 # output: 13 6 # output: 14 0 # output: 15 1 # output: 16 2 # output: 17 3 # output: 18 4 # output: 19 5 class F7: def __init__(self,x): self.int = x % 7 def __str__(self): return str(self.int) __repr__ = __str__ # examples: print F7(2) # output: 2 print F7(6) # output: 6 print F7(7) # output: 0 print F7(10) # output: 3 F7.__add__ = lambda a,b: F7(a.int + b.int) F7.__sub__ = lambda a,b: F7(a.int - b.int) F7.__mul__ = lambda a,b: F7(a.int * b.int) # examples: print F7(2) + F7(5) # output: 0 print F7(2) - F7(5) # output: 4 print F7(2) * F7(5) # output: 3 F7.__eq__ = lambda a,b: a.int==b.int F7.__ne__ = lambda a,b: a.int!=b.int # examples: print F7(7) == F7(0) # output: True print F7(10) == F7(3) # output: True print F7(-3) == F7(4) # output: True print F7(0) != F7(1) # output: True print F7(0) != F7(2) # output: True print F7(1) != F7(2) # output: True print F7(7) != F7(0) # output: False print F7(10) != F7(3) # output: False print F7(-3) != F7(4) # output: False print F7(0) == F7(1) # output: False print F7(0) == F7(2) # output: False print F7(1) == F7(2) # output: False 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 # examples: F1009 = F(1009) print F1009(-1) == F1009(1008) print F1009(6) != F1009(5) print F1009(101) + F1009(1000) == F1009(92) print F1009(101) - F1009(1000) == F1009(110) print F1009(101) * F1009(1000) == F1009(100) print F1009(101) / F1009(1000) == F1009(213)