-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script can now work with all the numbers of lenght n and base 10
- Loading branch information
1 parent
7aafe26
commit 5d35869
Showing
1 changed file
with
20 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
def is_armstrong_number(number): | ||
total = 0 | ||
""" | ||
In number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong) or a plus perfect number), | ||
in a given number base b, is a number that is the total of its own digits each raised to the power of the number of digits. | ||
Source: https://en.wikipedia.org/wiki/Narcissistic_number | ||
NOTE: | ||
this scripts only works for number in base 10 | ||
""" | ||
|
||
# find the sum of the cube of each digit | ||
temp = number | ||
while temp > 0: | ||
digit = temp % 10 | ||
total += digit ** 3 | ||
temp //= 10 | ||
def is_armstrong_number(number:str): | ||
total:int = 0 | ||
exp:int = len(number) #get the number of digits, this will determinate the exponent | ||
|
||
digits:list[int] = [] | ||
for digit in number: digits.append(int(digit)) #get the single digits | ||
for x in digits: total += x ** exp #get the power of each digit and sum it to the total | ||
|
||
# return the result | ||
if number == total: | ||
return True | ||
# display the result | ||
if int(number) == total: | ||
print(number,"is an Armstrong number") | ||
else: | ||
return False | ||
print(number,"is not an Armstrong number") | ||
|
||
number = int(input("Enter the number: ")) | ||
if is_armstrong_number(number): | ||
print(number,"is an Armstrong number") | ||
else: | ||
print(number,"is not an Armstrong number") | ||
number = input("Enter the number : ") | ||
is_armstrong_number(number) |