dvdktn dvdktn - 1 month ago 19
Python Question

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.

Answer

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))