parfait.py

Created by alexandrebret-84

Created on October 16, 2021

636 Bytes

parf(n) pour trouver les nombres parfaits entre 1 et n.


def div_strict(n:int):
    if n == 0:
        return "0 a une infinité de diviseurs dont 42"
    d = []
    if n > 0:
        for i in range(1, n+1):
            if n % i == 0:
                d.append(i)
        d.remove(d[-1])
        return d
    
    
def parf(n:int):
    if n < 0:
        return "Un nombre parfait ne peut être que positif"
    if n == 0:
        return None
    nb_parf = []
    for i in range(1, n+1):
        a = 0
        d = div_strict(i)
        for j in range(len(d)):
            a += d[j]
        if a == i:
            nb_parf.append(i)
        else:
            pass
    return nb_parf

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.