We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int isPrime(int n) {
if (n < 2) return 0;
for (int i = 2; i*i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
int digitSum(int n) {
int s = 0;
while (n) s += n % 10, n /= 10;
return s;
}
int primeFactorDigitSum(int n) {
int s = 0;
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
while (n % i == 0) {
s += digitSum(i);
n /= i;
}
}
}
return s;
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", (!isPrime(n) && digitSum(n) == primeFactorDigitSum(n)) ? 1 : 0);
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Identify Smith Numbers
You are viewing a single comment's thread. Return to all comments →
simple code
include
int isPrime(int n) { if (n < 2) return 0; for (int i = 2; i*i <= n; i++) if (n % i == 0) return 0; return 1; }
int digitSum(int n) { int s = 0; while (n) s += n % 10, n /= 10; return s; }
int primeFactorDigitSum(int n) { int s = 0; for (int i = 2; i <= n; i++) { if (isPrime(i)) { while (n % i == 0) { s += digitSum(i); n /= i; } } } return s; }
int main() { int n; scanf("%d", &n); printf("%d\n", (!isPrime(n) && digitSum(n) == primeFactorDigitSum(n)) ? 1 : 0); return 0; }