Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions maths/armstrong_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ def armstrong_number(n: int) -> bool:
temp //= 10
return n == sum

def narcissistic_number(n:int) -> bool:
"""Return True if n is a armstrong 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():
"""
Request that user input an integer and tell them if it is Armstrong number.
"""
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 narcissistic_number(num) else 'not '}an Armstrong number.")


if __name__ == "__main__":
Expand Down