Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python abundant, deficient, or perfect number

def classify(numb):
    i=1
    j=1
    sum=0
    for i in range(numb):
        for j in range(numb):
            if (i*j==numb):
                sum=sum+i
                sum=sum+j
            if sum>numb:
                print("The value",numb,"is an abundant number.")
            elif sum<numb:
                print("The value",numb,"is a deficient number.")
            else:
                print("The value",numb,"is a perfect number.")
            break
    return "perfect"

The code takes a number(numb) and classifies it as an abundant, deficient or perfect number. My output is screwy and only works for certain numbers. I assume it's indentation or the break that I am using incorrectly. Help would be greatly appreciated.

like image 571
dvdktn Avatar asked Aug 09 '26 16:08

dvdktn


1 Answers

I would highly recommend u to create a one function which creates the proper divisor of given N, and after that, the job would be easy.

def get_divs(n):
    return [i for i in range(1, n) if n % i == 0]


def classify(num):
    divs_sum = sum(get_divs(num))
    if divs_sum > num:
        print('{} is abundant number'.format(num))
    elif divs_sum < num:
        print('{} is deficient number'.format(num))
    elif divs_sum == num:
        print('{} is perfect number'.format(num))
like image 172
Nf4r Avatar answered Aug 11 '26 05:08

Nf4r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!