Narcissistic Number
Given an integer n
, return whether it is equal to the sum of its own digits raised to the power of the number of digits.
https://binarysearch.com/problems/Narcissistic-Number
ExamplesPermalink
Example 1Permalink
Input
- n =
153
Output
- answer =
True
Explanation
153 = 1 ** 3 + 5 ** 3 + 3 ** 3
SolutionPermalink
bool solve(int n) | |
{ | |
int nDigits = to_string(n).length(); | |
int running = 0; | |
int oriN = n; | |
while (n > 0) | |
{ | |
running += pow(n % 10, nDigits); | |
n /= 10; | |
} | |
return running == oriN; | |
} |
Leave a comment