比较水,质因数分解然后各位求和
#include<stdio.h>
#include<math.h>
int digit_sum(int num)
{
int sum = 0;
while (num)
{
sum += num % 10;
num /= 10;
}
return sum;
}
int factor_sum(int num)
{
int i, n = (int) sqrt(num);
for (i = 2; i <= n; i++)
if (num % i == 0)
return digit_sum(i) + factor_sum(num / i);
return digit_sum(num);
}
int is_prime(int num)
{
int i, n = (int) sqrt(num);
for (i = 2; i <= n; i++)
if (num % i == 0)
return 0;
return 1;
}
int main()
{
int n;
while (scanf("%d", &n), n)
{
int num = n + 1;
while (is_prime(num) || factor_sum(num) != digit_sum(num))
num++;
printf("%d\n", num);
}
return 0;
}