Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  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:

    • 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:

    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") 
    
like image 264
Angel Valenzuela Avatar asked Jul 04 '18 08:07

Angel Valenzuela


Video Answer


1 Answers

I dont know how efficient the code is but it does produce the desired result :

def facto():
  minr=int(input('enter the minimum range  :'))        #asking minimum range
  maxr=int(input('enter the range maximum range :'))   #asking maximum range
  i=minr
  while i <= maxr :
    l2=[]
    k=str(i)
    k=list(k)        #if i=[1,4,5]
    for n in k:      #taking each element
        fact=1
        while int(n) > 0:  #finding factorial of each element
          n=int(n)
          fact=fact*n
          n=n-1

        l2.append(fact) #keeping factorial of each element eg : [1,24,120]
    total=sum(l2)       # taking the sum of l2 list eg 1+24+120 = 145
    if total==i: #checking if sum is equal to the present value of i.145=145
        print(total) # if sum = present value of i than print the number
    i=int(i)
    i=i+1

facto()

input : minr =0 , maxr=99999 output : 1 2 145 40585

like image 174
Milan Mangar Avatar answered Sep 20 '22 15:09

Milan Mangar