Archive

Archive for June 23, 2019

Python: Perfect Number



Python: Non-abundant sums
Problem No.23 @ ProjectEuler

In projecteuler Problem No. 23 talking about Non-abundant sums to clear this it state three categories for a numbers.

1. A number N called Perfect Number if the sum of its divisors are equal to the number it self. So 28 is a perfect number because the divisors of 28 are 1,2,4,7,14 they equal to 28.
( 1 + 2 + 4 + 7 + 1 4 = 28)

2. A number N is called Deficient if the sum of its proper divisors is less than the number it self N.

3. And a number N is called Abundant if this sum of its proper divisors is exceeds N (number it self)

Enhancement: So instead of solving problem No.23, we will write a code to determent the group that a number belongs to. As we trying to make a general cods, we will ask the user to enter a number then we will call the function to collect all the divisors of N and get its sum and gives it a name according to the classification we just talk about.



The Code:


# Python: Non-abundant sums
# Problem No.23 @ ProjectEuler
# We solve it in my way

num_divisors=[]

def get_num_group (num):

for x in range(1,num):

if num%x == 0:

num_divisors.append(x)

num =int(input(‘Enter a number: ‘))
div_sum =0

get_num_group(num)

for each in num_divisors:

div_sum = div_sum + each

print(‘\n Divisors of {} are,’.format(num),num_divisors)
print(‘ The sum of the Divisors is ‘,div_sum)
if div_sum == num :

print (‘ The number {} is Perfect Number’.format(num))
elif div_sum < num :

print (‘ The number {} is Deficient ‘.format(num))
else:

print (‘ The number {} is Abundant ‘.format(num))






Follow me on Twitter..



Advertisements