Last active
November 17, 2016 21:45
-
-
Save davidventuri/68a5695849dec41671a6410fd444d1f6 to your computer and use it in GitHub Desktop.
Returns true if x is a narcissistic number.
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
"""A _narcissistic number_ is an `n`-digit whole number that is equal to the | |
sum of its digits raised to the power `n`. | |
For instance, `153` is such a 3-digit number (`1^3 + 5^3 + 3^3 = 153`) | |
and `8208` is a 4-digit example (`8^4+ 2^4 + 0^4 + 8^4 = 8208`). | |
Write a function `is_narcissistic(x)` that returns _true_ if `x` is | |
narcissistic. Now use this to find as many narcissistic numbers as you can. | |
How high up can you go? | |
Note: 1-digit numbers are all trivially narcissistic, so you may skip them. | |
""" | |
def is_narcissistic(x): | |
digits = [int(i) for i in str(x)] | |
len_num = len(digits) | |
sum_digits_raised_len = 0 | |
for digit in digits: | |
sum_digits_raised_len += digit ** len_num | |
if sum_digits_raised_len == x: | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment