mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
added method for checking armstrong number (#1936)
* added method for checking armstrong number * Update comment Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
b6fcee3114
commit
f80ffe1f54
|
@ -41,6 +41,14 @@ def armstrong_number(n: int) -> bool:
|
||||||
temp //= 10
|
temp //= 10
|
||||||
return n == sum
|
return n == sum
|
||||||
|
|
||||||
|
def narcissistic_number(n:int) -> bool:
|
||||||
|
"""Return True if n is a narcissistic number or False if it is not"""
|
||||||
|
|
||||||
|
expo = len(str(n)) #power, all number will be raised to
|
||||||
|
temp = [(int(i)**expo) for i in str(n)] # each digit will be multiplied expo times
|
||||||
|
|
||||||
|
# check if sum of cube of each digit is equal to number
|
||||||
|
return n == sum(temp)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
|
@ -48,6 +56,7 @@ def main():
|
||||||
"""
|
"""
|
||||||
num = int(input("Enter an integer to see if it is an Armstrong number: ").strip())
|
num = int(input("Enter an integer to see if it is an Armstrong number: ").strip())
|
||||||
print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.")
|
print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.")
|
||||||
|
print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user