UVALive2247 Prime Digital Roots【水题】

本文介绍了一种特殊的数根计算方法——素数数根。该方法不仅计算整数的数根,还要求数根本身必须是素数。文章详细解释了素数数根的概念,并通过示例展示了计算过程。此外,还提供了一个完整的C语言程序实现。

  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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值