How can I display all numbers in range 0-N that are “super numbers”

  • A+
Category:Languages

1.) The program asks the user for a number N

2.) The program is supposed to displays all numbers in range 0-N that are "super numbers".

Super number: is a number such that the sum of the factorials of its digits equals the number.

examples of super numbers.

12 != 1! + 2! = 1 + 2 = 3 (it's not super) 145 = 1! + 4! + 5! = 1 + 24 + 120 (is super) 

3.) The part I seem to be stuck at is when the program displays all numbers in range 0-N that are "super numbers". I have concluded I need a loop in order to solve this but I do not know how to go about it So, for example, the program is supposed to read all the numbers from 0-50 and whenever the number is super it displays it. so it only displays 1 and 2 since they are considered super

enter integer: 50 2 is super 1 is super 

4.) I have written two functions the first is a regular factorial program and the Second is a program that sums the factorials of the digits my code

number = int(input ("enter integer: "))  def factorial (n):     result = 1      i =n *(n-1)     while n >= 1:         result = result * n         n = n-1     return result  #print(factorial(number))  def breakdown (n):     breakdown_num = 0     remainder = 0     if n < 10:         breakdown_num += factorial(n)         return breakdown_num      else:         while n > 10:             digit = n % 10             remainder = n // 10              breakdown_num += factorial(digit)              #print (str(digit))             #print(str(breakdown_num))             n = remainder              if n < 10 :                 #print (str(remainder))                 breakdown_num += factorial(remainder)                 #print (str(breakdown_num))         return breakdown_num  #print(breakdown(number)) if (breakdown(number)) == number:     print(str(number)+ " is super")  

 


First I would slightly change how main code is executed, by moving main parts to if __name__ == '__main__', which will execute after running this .py as main file:

if __name__ == '__main__':     number = int(input ("enter integer: "))     if (breakdown(number)) == number:         print(str(number)+ " is super") 

After that it seems much clearer what you should do to loop over numbers, so instead of above it would be:

if __name__ == '__main__':     number = int(input ("enter integer: "))     for i in range(number+1):         if (breakdown(i)) == i:             print(str(i)+ " is super") 

Example input and output:

enter integer: 500 1 is super 2 is super 145 is super 

Small advice - you don't need to call str() in print() - int will be shown the same way anyway

Hope that helps

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: