-1
I’m having a hard time doing isPerfect(x)
work. I’ve tried some other options but I don’t know if my logical reasoning is wrong.
Perfect number
A number is said to be Perfect if it is Equal to the sum of all its factors (for obvious reasons the list of factors being considered does not include the number itself).
6 = 3 + 2 + 1
, Hence 6 is Perfect.)
def getFactors(x):
num_factors = []
for i in range(1, x +1):
if (x % i == 0):
num_factors.append(i)
return num_factors
def isPrime(x):
return len(getFactors(x)) == 2
def isComposite(x):
return False if isPrime(x) else True
def isPerfect(x):
return isPerfect(x)
if sum(getFactors(x)) == (getFactors(x))
else:
return False
def main():
playing = True
while playing == True:
num_input = input('Give me a number from 1 to 10000. Type -1 to exit. ')
try:
num = int(num_input)
if (num == -1):
playing = False
continue
if (num <= 0 or num > 10000):
continue
factors = getFactors(num)
print("The factors of", num, "are", factors)
if isPrime(num):
print(str(num) + ' is prime')
if isComposite(num):
print(str(num) + ' is composite')
if isPerfect(num):
print(str(num) + ' is perfect')
except ValueError:
print('Sorry, the input is not an int. Please try again.')
#This will automatically run the main function in your program
#Don't change this
if __name__ == '__main__':
main()
Welcome to Sopt. Your question is a little vague, which part isn’t working? You can edit your question and add more information.
– Pedro Sanção