12 from __future__
import division
13 from __future__
import print_function
16 VERSION =
'intmod --- 2010 March 16'
30 """Type nombre entier modulo k (c.-à-d. un élément de Z_k) :
31 0, 1, 2, …, k-1"""
37 Pre: value: Integral ou String
39 assert isinstance(value, numbers.Integral)
or isinstance(value, str), value
43 if isinstance(value, str):
44 value = int(value, base=base)
46 assert isinstance(value, numbers.Integral), value
48 return int.__new__(cls, value%k)
53 """Initialise l'élément à self%k
55 Pre: value: Integral ou String
57 assert isinstance(value, numbers.Integral)
or isinstance(value, str), value
67 """Renvoie self + other (modulo self.k())
69 Pre: other: Intmod ou Integral
71 Result: Intmod(, k=self.k())"""
72 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
74 return Intmod(int.__add__(self, other), self.
_k)
79 """Renvoie le naturel correspondant à la valeur self
81 Result: Integral < self.k()"""
82 return int.__int__(self)
87 """Renvoie ~self (modulo self.k())
89 Result: Intmod(, k=self.k())"""
90 return Intmod(int.__invert__(self), self.
_k)
95 """Renvoie self<<other (modulo self.k())
97 Pre: other: Intmod ou (Integral >= 0)
99 Result: Intmod(, k=self.k())"""
100 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
102 return Intmod(int.__lshift__(self, other), self.
_k)
107 """Renvoie self*other (modulo self.k())
109 Pre: other: Intmod ou Integral
111 Result: Intmod(, k=self.k())"""
112 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
114 return Intmod(int.__mul__(self, other), self.
_k)
119 """Renvoie -self (modulo self.k())
121 Result: Intmod(, k=self.k())"""
122 return Intmod(int.__neg__(self), self.
_k)
127 """Renvoie self | other (modulo self.k())
129 Pre: other: Intmod ou Integral
131 Result: Intmod(, k=self.k())"""
132 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
134 return Intmod(int.__or__(self, other), self.
_k)
139 """Renvoie self**other (modulo self.k())
141 Pre: other: Intmod ou Integral
143 Result: Intmod(, k=self.k())"""
144 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
146 return Intmod(int.__pow__(self, (other
if isinstance(other, Intmod)
or (other >= 0)
147 else self.
_k - other), self.
_k), self.
_k)
152 """Renvoie self - other (modulo self.k())
154 Pre: other: Intmod ou Integral
156 Result: Intmod(, k=self.k())"""
157 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
159 return Intmod(int.__sub__(self, other), self.
_k)
164 """Renvoie self ^ other (modulo self.k())
166 Pre: other: Intmod ou Integral
168 Result: Intmod(, k=self.k())"""
169 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
171 return Intmod(int.__xor__(self, other), self.
_k)
176 """Renvoie la valeur du modulo k
178 Result: Integral >= 1"""
187 """Renvoie (self//other, self%other) (modulo self.k())
189 Pre: other: Intmod ou Integral
191 Result: NotImplemented"""
192 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
194 return NotImplemented
199 """Renvoie self//other (modulo self.k())
201 Pre: other: Intmod ou Integral
203 Result: NotImplemented"""
204 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
206 return NotImplemented
211 """Renvoie self%other (modulo self.k())
213 Pre: other: Intmod ou Integral
215 Result: NotImplemented"""
216 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
218 return NotImplemented
223 """Renvoie NotImplemented
225 Pre: other: Intmod ou Integral
227 Result: NotImplemented"""
228 assert isinstance(other, Intmod)
or isinstance(other, numbers.Integral), other
230 return NotImplemented
237 if __name__ ==
'__main__':
244 debug.test_begin(VERSION, __debug__)
246 print(
'Intmod()...', end=
'') ; sys.stdout.flush()
247 for k
in range(1, 11):
248 for n
in range(-k*2, k*3):
250 assert a._k == k, (k, n, a, a_k)
251 assert int(a) == n%k, (k, n, a, int(a))
252 print(
'ok') ; sys.stdout.flush()
255 print(
'Intmod.__add__()...', end=
'') ; sys.stdout.flush()
256 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
257 for a
in range(-k*2, k*3):
259 for b
in range(-k, k*2):
262 assert a + b == int(a + b)%k, (k, a, b, a + b, int(a + b)%k)
263 assert a + b == b + a, (k, a, b)
264 for b
in range(-k, k*2):
265 assert a + b == int(a + b)%k, (k, a, b, a + b, int(a + b)%k)
266 print(
'ok') ; sys.stdout.flush()
269 print(
'Intmod.__and__()...', end=
'') ; sys.stdout.flush()
270 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
271 for a
in range(-k*2, k*3):
273 for b
in range(-k, k*2):
276 assert a & b == int(a & b)%k, (k, a, b, a & b, int(a & b)%k)
277 assert a & b == b & a, (k, a, b)
278 for b
in range(-k, k*2):
279 assert a & b == int(a & b)%k, (k, a, b, a & b, int(a & b)%k)
280 print(
'ok') ; sys.stdout.flush()
283 print(
'Intmod.__divmod__()...', end=
'') ; sys.stdout.flush()
284 for k
in range(1, 5):
285 for a
in range(-k*2, k*2):
287 for b
in range(-k, k*2):
290 assert divmod(a, b), (k, a, b)
293 print(
'ok') ; sys.stdout.flush()
296 print(
'Intmod.__floordiv__()...', end=
'') ; sys.stdout.flush()
297 for k
in range(1, 5):
298 for a
in range(-k*2, k*2):
300 for b
in range(-k, k*2):
303 assert a//b, (k, a, b)
306 print(
'ok') ; sys.stdout.flush()
309 print(
'Intmod.__int__()...', end=
'') ; sys.stdout.flush()
310 for k
in range(1, 11):
311 for n
in range(-k*2, k*3):
313 assert a == n%k, (k, n, a)
314 print(
'ok') ; sys.stdout.flush()
317 print(
'Intmod.__invert__()...', end=
'') ; sys.stdout.flush()
318 for k
in range(1, 11):
319 for a
in range(-k*2, k*3):
321 assert ~a == int(~a)%k, (k, a, ~a, int(~a)%k)
322 print(
'ok') ; sys.stdout.flush()
325 print(
'Intmod.__lshift__()...', end=
'') ; sys.stdout.flush()
326 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
327 for a
in range(-k*2, k*3):
329 for b
in range(-k, k*2):
332 assert a<<b == int(a<<b)%k, (k, a, b, a<<b, int(a<<b)%k)
334 assert a<<b == int(a<<b)%k, (k, a, b, a<<b, int(a<<b)%k)
335 print(
'ok') ; sys.stdout.flush()
338 print(
'Intmod.__mod__()...', end=
'') ; sys.stdout.flush()
339 for k
in range(1, 5):
340 for a
in range(-k*2, k*2):
342 for b
in range(-k, k*2):
345 assert a%b, (k, a, b)
348 print(
'ok') ; sys.stdout.flush()
351 print(
'Intmod.__mul__()...', end=
'') ; sys.stdout.flush()
352 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
353 for a
in range(-k*2, k*3):
355 for b
in range(-k, k*2):
358 assert a*b == int(a*b)%k, (k, a, b, a*b, int(a*b)%k)
359 assert a*b == b*a, (k, a, b)
360 for b
in range(-k, k*2):
361 assert a*b == int(a*b)%k, (k, a, b, a*b, int(a*b)%k)
362 print(
'ok') ; sys.stdout.flush()
365 print(
'Intmod.__neg__()...', end=
'') ; sys.stdout.flush()
366 for k
in range(1, 11):
367 for a
in range(-k*2, k*3):
369 assert -a == int(-a)%k, (k, a, -a, int(-a)%k)
370 print(
'ok') ; sys.stdout.flush()
373 print(
'Intmod.__or__()...', end=
'') ; sys.stdout.flush()
374 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
375 for a
in range(-k*2, k*3):
377 for b
in range(-k, k*2):
380 assert a | b == int(a | b)%k, (k, a, b, a | b, int(a | b)%k)
381 assert a | b == b | a, (k, a, b)
382 for b
in range(-k, k*2):
383 assert a | b == int(a | b)%k, (k, a, b, a | b, int(a | b)%k)
384 print(
'ok') ; sys.stdout.flush()
387 print(
'Intmod.__pow__()...', end=
'') ; sys.stdout.flush()
388 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
389 for a
in range(-k*2, k*3):
391 for b
in range(-k, k*2):
394 assert a**b == int(a**b)%k, (k, a, b, a**b, int(a**b)%k)
395 for b
in range(-k, k*2):
396 assert a**b == int(a**b)%k, (k, a, b, a**b, int(a**b)%k)
397 print(
'ok') ; sys.stdout.flush()
400 print(
'Intmod.__rshift__()...', end=
'') ; sys.stdout.flush()
401 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
402 for a
in range(-k*2, k*3):
404 for b
in range(-k, k*2):
407 assert a>>b == int(a>>b)%k, (k, a, b, a>>b, int(a>>b)%k)
409 assert a>>b == int(a>>b)%k, (k, a, b, a>>b, int(a>>b)%k)
410 print(
'ok') ; sys.stdout.flush()
413 print(
'Intmod.__str__()...', end=
'') ; sys.stdout.flush()
414 for k
in range(1, 11):
415 for n
in range(-k*2, k*3):
417 assert str(a) == str(n%k), (k, n, str(a))
418 print(
'ok') ; sys.stdout.flush()
421 print(
'Intmod.__sub__()...', end=
'') ; sys.stdout.flush()
422 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
423 for a
in range(-k*2, k*3):
425 for b
in range(-k, k*2):
428 assert a - b == int(a - b)%k, (k, a, b, a - b, int(a - b)%k)
429 assert a - b == -(b - a), (k, a, b)
430 for b
in range(-k, k*2):
431 assert a - b == int(a - b)%k, (k, a, b, a - b, int(a - b)%k)
432 print(
'ok') ; sys.stdout.flush()
435 print(
'Intmod.__truediv__()...', end=
'') ; sys.stdout.flush()
436 for k
in range(1, 5):
437 for a
in range(-k*2, k*2):
439 for b
in range(-k, k*2):
442 assert a/b, (k, a, b)
445 print(
'ok') ; sys.stdout.flush()
448 print(
'Intmod.__xor__()...', end=
'') ; sys.stdout.flush()
449 for k
in range(1, 11
if debug.assertspeed >= debug.ASSERT_NORMAL
else 5):
450 for a
in range(-k*2, k*3):
452 for b
in range(-k, k*2):
455 assert a ^ b == int(a ^ b)%k, (k, a, b, a ^ b, int(a ^ b)%k)
456 assert a ^ b == b ^ a, (k, a, b)
457 for b
in range(-k, k*2):
458 assert a ^ b == int(a ^ b)%k, (k, a, b, a ^ b, int(a ^ b)%k)
459 print(
'ok') ; sys.stdout.flush()
462 print(
'Intmod.k()...', end=
'') ; sys.stdout.flush()
463 for k
in range(1, 11):
464 for a
in range(-k*2, k*3):
466 assert a.k() == k, (k, a, a.k())
467 print(
'ok') ; sys.stdout.flush()