arithmetique.py

Created by 1epauletteshark

Created on December 06, 2023

2.09 KB


from math import floor,sqrt

def is_prime(a):
  if a<=1:
    return False
  if a==2:
    return True
  if a%2==0:
    return False
  max=floor(sqrt(a))
  for d in range(3,max+1,2):
      if a%d==0:
        return False
  return True

def prime_range(a,b):
  c=[]
  print("-"*43)
  for i in range(a,b):
    if is_prime(i):
      c.append(i)
  print(c)
  print("-"*43)

def factorise(a):
  factors=[]
  i=2
  while i*i<=a:
    if a%i==0:
      a//=i
      factors.append(i)
    else:
      i+=1
  if a>1:
    factors.append(a)
  return factors

def simplify(a,b):
  fa=factorise(a)
  fb=factorise(b)
  fc=[]
  print("-"*43)
  print("{} = {}".format(a,fa))
  print("{} = {}".format(b,fb))
  print("-"*43)
  cf=set(fa)&set(fb)
  for i in list(fa):
    if i in fb:
      fa.remove(i)
      fb.remove(i)
      fc.append(i)
  print("Facteurs commun: {}".format(fc))
  print("-"*43)
  ra=1
  rb=1
  for f in fa:
    ra*=f
  for f in fb:
    rb*=f
  print("Fraction simplifié:")
  print("{} / {} = {} / {}".format(a,b,ra,rb))
  print("-"*43)

def divisors(a):
  return [i for i in range(1,a+1) if a%i==0]

def euclidean(a,b):
  print("-"*43)
  print("{}={}*{}+{}".format(a,b,int(a//b),int(a%b)))
  print("-"*43)

def sync(a,b):
  print("-"*43)
  t=0
  while True:
    t+=min(a,b)
    if t%a==0 and t%b==0:
      print("{} = {} x {}".format(t,a,int(t/a)))
      print("{} = {} x {}".format(t,b,int(t/b)))
      print("-"*43)
      break

def gcd(a,b):
  fa=factorise(a)
  fb=factorise(b)
  fc=[]
  print("-"*43)
  print("{} = {}".format(a,fa))
  print("{} = {}".format(b,fb))
  print("-"*43)
  for i in list(fa):
    if i in fb:
      fa.remove(i)
      fb.remove(i)
      fc.append(i)
  print("Facteurs commun: {}".format(fc))
  print("-"*43)
  scf=1
  for i in fc:
    scf*=i
  print("{} = {} * {}".format(a,scf,int(a/scf)))
  print("{} = {} * {}".format(b,scf,int(b/scf)))
  print("-"*43)

print("-"*43)
print("Arithmetic 3eme")
print("-"*43)
print("is_prime(a)")
print("prime_range(a,b)")
print("factorise(a)")
print("gcd(a,b)")
print("simplify(a,b)")
print("divisors(a)")
print("euclidean(a,b)")
print("sync(a,b)")
print("-"*43)

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.