arithmetique.py

Created by loupiot

Created on April 24, 2018

1.11 KB

fonctions pour l’arithmétique de terminale S spé maths. Merci à Bisam de ti planet à qui j’ai usurpé la fonction bezout(a,b)


from math import*

def isprime(n):
  if type(n)!=int or n<2:
    return False
  for i in range(2,int(n**0.5)+1):
    if n%i==0:
      return False
  return True

def diviseurs(n):
  if type(n)!=int or n<1:
    return None
  if n==1:
    return [1]
  liste=[1]
  for i in range(2,int(n**0.5)+1):
    if n%i==0:
      liste.append(i)
      if n//i!=i:
        liste.append(n//i)
  return sorted(liste)+[n]

def pgcd(a,b):
  if type(a+b)!=int:
    return None
  while b!=0:
    a,b=b,a%b
  return abs(a)

def ppcm(a,b):
  if type(a+b)!=int:
    return None
  return a*b//pgcd(a,b)

def quotient(a,b):
  if type(a+b)!=int:
    return None
  liste=[]
  while b!=0:
    liste.append(a//b)
    a,b=b,a%b
  return liste

def bezout(a,b):
  if pgcd(a,b)!=1:
    return None
  u0, v0, u1, v1 = 1, 0, 0, 1
  while b:
    a, (q, b) = b, divmod(a, b)
    u0, v0, u1, v1 = u1, v1, u0 - q * u1, v0 - q * v1
  return u0, v0

def diophant(a,b,c):
  d=pgcd(a,b)
  if pgcd(d,c)!=d:
    return None
  if abs(a)<abs(b):
    a,b=b,a
  e,f,g=a//d,b//d,c//d
  u,v=bezout(e,f)
  print("(",u*g,"+",f,"k)*",a,"+","(",v*g,"-",e,"k)*",b,"=",c)

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.