Skip to content

added method for checking armstrong number #1936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from May 3, 2020
Merged
Changes from all commits
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 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():
"""
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