Skip to content

Instantly share code, notes, and snippets.

@squiidz
Created March 27, 2017 13:20
Show Gist options
  • Save squiidz/ba9ce393f3fdfced015e8e15b6c3c6f9 to your computer and use it in GitHub Desktop.
Save squiidz/ba9ce393f3fdfced015e8e15b6c3c6f9 to your computer and use it in GitHub Desktop.
narcissistic numbers
#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