divisors.py

Created by alain-busser

Created on April 30, 2018

266 Bytes

Exemples d’utilisation ici: http://irem.univ-reunion.fr/spip.php?article970

divisors(n) yields the list of the divisors of an integer n;

cd(a,b) (“common divisors”) yields the lists of the common divisors of a and b;

gcd(a,b) yields the greatest of the common divisors

and isPrime(n) is a test for the primality of the integer n.


def inter(l1,l2):
  return [x for x in l1 if x in l2]
def divisors(n):
  return [d for d in range(1,n+1) if n%d==0]
def isPrime(n):
  return len(divisors(n))==2
def cd(a,b):
  return inter(divisors(a),divisors(b))
def gcd(a,b):
  return max(cd(a,b))

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.