Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # SpecialNumber.py
- # Python 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
- # Chris Clarke
- # 24.10.2015
- def factorial(n):
- ans = 1
- for x in range(2, n+1):
- ans *= x
- return ans
- def isSpecial(num):
- num2 = num
- sum = 0
- while num2>0:
- n = num2 % 10 # modulus = remainder
- sum += factorial(n) # add n! to sum
- if sum > num:
- return False
- num2 = num2 // 10 # integer division
- if sum == num:
- return True
- return False
- def main():
- x = int (input ("Enter number: "))
- if isSpecial(x):
- print("%d is a special number!" % x)
- else:
- print("%d is not special." % x)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement