systeme_congruences.py

Created by nicolas-patrois

Created on June 09, 2018

500 Bytes

Résout les systèmes de congruences en utilisant le théorème des restes chinois.

[[1,3],[0,25],[3,16]] <=> x=1[3], x=0[25] et x=3[16] de solution 1075


def prod(l):
 return eval("*".join(map(str,l)))

def bezout(n,a):
 r,u,rr,uu=a,1,n,0
 while rr:
  q=r//rr
  r,u,rr,uu=rr,uu,r-q*rr,u-q*uu
 return u%n

def pgcd(a,b):
 a,b=max(a,b),min(a,b)
 while a:
  b,a=a,b%a
 return b

def trc(l):
 p=pgcd(l[0][1],l[1][1])
 if (l[0][0]-l[1][0])%p:
  return None
 n=prod([a[1] for a in l])
 q=[n//l[i][1] for i in range(len(l))]
 v=[bezout(l[i][1],q[i]) for i in range(len(l))]
 return sum([l[j][0]*q[j]*v[j] for j in range(len(l))])%n//p

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.