fork download
  1. #import gmpy2
  2.  
  3. '''
  4. Given the following set of linear congruences:
  5.  
  6. x ≡ 2 mod 5
  7. x ≡ 3 mod 11
  8. x ≡ 5 mod 17
  9.  
  10. Find the integer a such that x ≡ a mod 935
  11. '''
  12.  
  13. from Crypto.Util.number import *
  14.  
  15. def Chinese(n, N, a):
  16. result = 0
  17.  
  18. for i in range(len(n)):
  19. ai = a[i]
  20. ni = n[i]
  21. bi = N // ni
  22.  
  23. result += ai * bi * inverse(bi, ni)
  24.  
  25. return result % N
  26.  
  27. N=935
  28. n=[5,11,17]
  29. a=[2,3,5]
  30. result=Chinese(n,N,a)
  31. print(result)
  32.  
Success #stdin #stdout 0.02s 8340KB
stdin
Standard input is empty
stdout
872