arithmetique_divers.py

Created by elodie-gamot

Created on June 10, 2022

1.43 KB

Divers scripts pour lister les diviseurs d’un nombre, déterminer si un nombre est premier ou non, calculer le PGCD de deux nombres, déterminer si des nombres sont amicaux ou non, etc etc.


from math import *

def diviseurs(n):
  div=[]
  for k in range(1,n+1):
    if n%k==0:
      div.append(k)
  return div

def diviseurs_short(n):
  return [k for k in range(1,n+1) if n%k==0]

def pgcd(a,b):
  liste1=diviseurs(a)
  liste2=diviseurs(b)
  communs=[]
  for i in range(len(liste1)):
    if liste1[i] in liste2:
      communs.append(liste1[i])
  return max(communs)

def pgcd_short(a,b):
  liste1=diviseurs(a)
  liste2=diviseurs(b)
  communs=[i for i in liste1 if i in liste2]
  return max(communs)
  
def nombre_premier(n):
  if n>1:
    for k in range(2,sqrt(n)+1):
      if k<n and n%k==0:
        #need k<n if n=2
        return False
    return True

def liste_premiers(n):
  liste=[]
  i=2
  while len(liste)<n:
    if nombre_premier(i)==True:
      liste.append(i)
    i+=1
  return liste  

def liste_premiers_short(n):
  return [k for k in range(1,n+1) if nombre_premier(k)==True]
          
def somme_diviseurs(n):
  somme=0
  for k in range(1,n):
    if n%k==0:
      somme+=k
  return somme
  
def sum_div(n):
  div=[]
  for i in range(1,n):
    if n%i==0:
      div.append(i)
  return sum(div)
                                
def sum_div_short(n):
  return sum([k for k in range(1,n) if n%k==0])

def nb_parfait(n):
  if somme_diviseurs(n)==n:
    return True
  else:
    return False

def nb_amicaux(m,n):
  if somme_diviseurs(n)==m and somme_diviseurs(m)==n:
    return "Amicaux !"
  else:
    return "Non :("

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.