Advertisement
brilliant_moves

SpecialNumber.py

Oct 24th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. # SpecialNumber.py
  2. # Python 3
  3. # Input a number and check whether it is a special number or not. eg: 145 is a special no. as 1!+4!+5!=145
  4. # Chris Clarke
  5. # 24.10.2015
  6.  
  7. def factorial(n):
  8.     ans = 1
  9.     for x in range(2, n+1):
  10.         ans *= x
  11.     return ans
  12.  
  13. def isSpecial(num):
  14.     num2 = num
  15.     sum = 0
  16.     while num2>0:
  17.         n = num2 % 10       # modulus = remainder
  18.         sum += factorial(n) # add n! to sum
  19.         if sum > num:
  20.             return False
  21.         num2 = num2 // 10   # integer division
  22.     if sum == num:
  23.         return True
  24.     return False
  25.  
  26. def main():
  27.     x = int (input ("Enter number: "))
  28.     if isSpecial(x):
  29.         print("%d is a special number!" % x)
  30.     else:
  31.         print("%d is not special." % x)
  32.  
  33. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement