The digital root of a number is found by adding together the digits that make up the number. If theresulting number has more than one digit, the process is repeated until a single digit remains.
Your task in this problem is to calculate a variation on the digital root — a prime digital root. Theaddition process described above stops when there is only one digit left, but will also stop if the originalnumber, or any of the intermediate numbers (formed by addition) are prime numbers. If the processcontinues and results in a single digit that is not a prime number, then the original number has noprime digital root.
An integer greater than one is called a prime number if its only positive divisors (factors)are one and itself.
• For example, the first six primes are 2, 3, 5, 7, 11, and 13.
• Number 6 has four positive divisors: 6, 3, 2, and 1. Thus number 6 is not a prime.
• Caveat: number 1 is not a prime.
Examples of prime digital roots:
1 This is not a prime number, so 1 has no prime digital root.
3 This is a prime number, so the prime digital root of 3 is 3.
4 This not a prime number, so 4 has no prime digital root.
11 This is a prime number, so the prime digital root of 11 is 11.
642 This is not a prime number, so adding its digits gives 6 + 4 + 2 = 12. This is not a prime number,so adding again gives 1 + 2 = 3. This is a prime number, so the prime digital root of 642 is 3.
128 This is not a prime number, so adding its digits gives 1 + 2 + 8 = 11. This is a prime number, sothe prime digital root of 128 is 11.
886 This is not a prime number, so adding its digits gives 8 + 8 + 6 = 22. This is not a prime number,so adding again gives 2 + 2 = 4. This is not a prime number, so 886 has no prime digital root.
Input
The input will contain a single integer on each line in the range 0 to 999999 inclusive. The end of theinput will be indicated by the value ‘0’.
Output
If the input number has a prime digital root, then the input number must be output right aligned witha field width of 7. It must be followed by a single space, and then by the calculated prime digital rootalso right aligned with a field width of 7.
If the input number has no prime digital root, then the input number should be output as definedabove followed by 4 spaces followed by the word ‘none’ (in lowercase). The terminating zero shouldnot be output.
Sample Input
1
3
4
11
642
128
886
0
Sample Output
1 none
3 3
4 none
11 11
642 3
128 11
886 none
Regionals 2001 >> South Pacific
问题链接:UVALive2247 Prime Digital Roots。
问题分析:
数根是指整数的各个位的数字之和。如果其和为1位整数,则为结果;如果其和为多位整数,则再将各位数字相加,直到其和为1位数为止。
但是,这个题有点不一样,输入的数只要是素数就可以了,如果不是素数就计算其数根,数根(可以是多位整数)是素数也就可以了。必要时,需要重复计算数根,直到只剩下1位为止。
程序说明:
需要说明的有以下几点:
1.输入输出需要用64位整数,所以用了long long类型;
2.函数getroot()调用层次不会太深,所以使用递归调用;
3.输出时,两项宽度均为7,中间有个空格。
参考链接:HDU1013 POJ1519 UVALive2085 Digital Roots【数根+水题】
AC的C语言程序如下:
/* UVALive2247 Prime Digital Roots */
#include <stdio.h>
#include <math.h>
/* 试除法判断一个数是否为素数 */
int isprime(long long n)
{
if(n == 2 || n == 3)
return 1;
if((n & 1) == 0 || n == 1) /* 偶数:n % 2 == 0 */
return 0;
long end = sqrt(n), i;
for(i=3; i<=end; i+=2) {
if(n % i == 0)
break;
}
return i > end ? 1 : 0;
}
long getroot(long val)
{
int root = 0;
while(val) {
root += val % 10;
val /= 10;
}
while(root >= 10 && !isprime(root))
root = getroot(root);
return root;
}
int main(void)
{
long long n;
int root, okflag;
while(scanf("%lld", &n) != EOF) {
/* 判定结束条件 */
if(n == 0)
break;
/* 计算数根 */
if(isprime(n)) {
root = n;
okflag = 1;
} else {
root = getroot(n);
okflag = isprime(root);
}
if(okflag)
printf("%7lld%8d\n", n, root);
else
printf("%7lld none\n", n);
}
return 0;
}