Created
March 27, 2017 13:20
-
-
Save squiidz/ba9ce393f3fdfced015e8e15b6c3c6f9 to your computer and use it in GitHub Desktop.
narcissistic numbers
This file contains hidden or 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
#include <vector> | |
#include <math.h> | |
namespace number { | |
bool is_narci(int num) { | |
int num_cp = num; | |
int num_length = num == 1 ? 1 : ceil(log10(num)); | |
int length = num_length; | |
int acc = 0; | |
while (length != 0) { | |
double reduce = | |
(num % (int)pow(10, num_length)) == 0 ? 0 : 1; | |
int base = pow(10, (length - reduce)); | |
int diff = (num_cp / base); | |
acc += pow(diff, num_length); | |
num_cp -= (diff * base); | |
length -= 1; | |
} | |
return num == acc; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment