Carmichael Numbers

博客围绕判断Carmichael数展开,介绍了在密码学中判断大素数的难题及Fermat测试,引出Carmichael数概念。给出判断Carmichael数的条件,即非素数且满足a^n mod n = a,还阐述了素数打表和用快速幂算法判断的思路,并给出输入输出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography.

Alvaro is one of such persons, and is designing a set of cryptographic procedures for cooking paella. ´ Some of the cryptographic algorithms he is implementing make use of big prime numbers. However, checking if a big number is prime is not so easy. An exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square root. For big numbers, the amount of time and storage needed for such operations would certainly ruin the paella.

However, some probabilistic tests exist that offer high confidence at low cost. One of them is the Fermat test.

Let a be a random number between 2 and n−1 (being n the number whose primality we are testing). Then, n is probably prime if the following equation holds:

                                                  a^n mod n = a

If a number passes the Fermat test several times then it is prime with a high probability.

Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers. In this problem you are asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. As a side note, we need to mention that, according to Alvaro, the main advantage of encrypted ´ paella over conventional paella is that nobody but you knows what you are eating.

Input

The input will consist of a series of lines, each containing a small positive number n (2 < n < 65000). A number n = 0 will mark the end of the input, and must not be processed.

Output

For each number in the input, you have to print if it is a Carmichael number or not, as shown in the sample output.

Sample Input

1729

17

561

1109

431

0

Sample Output

The number 1729 is a Carmichael number.

17 is normal.

The number 561 is a Carmichael number.

1109 is normal.

431 is normal.

题意:

给你一个数字,让你判断这个数字是否是Carmichael number。

判断一个数是Carmichael number的条件是:1.不是素数,2.a^n mod n = a。

思路:

1.素数打表,找出来素数;

2.循环判断是否满足a^n mod n = a(要用到快速幂算法);

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

typedef long long ll;
const int N=65010;
int n;
int prise[N];

void Prise()//素数打表;等于1代表是偶数,等于0代表是素数;
{
    memset(prise,0,sizeof prise);
    for(int i=2; i<=N; i++)
    {
        if(prise[i]==0)
        {
            for(int j=2*i; j<=N; j=j+i)
            {
                prise[j]=1;
            }
        }
    }
    return ;
}

ll qq(ll x,ll n,ll mod)//快速幂算法;
{
    ll ret=1;
    while(n>0)
    {
        if(n&1)
            ret=ret*x%mod;
        x=x*x%mod;
        n>>= 1;
    }
    return ret;
}

int panduan()//判断是否满足a^n mod n = a;
{
    for(int i=2; i<n; i++)
        if(qq(i,n,n)!=i)
            return 0;
    return 1;
}

int main()
{
    Prise();
    while(~scanf("%d",&n))
    {
        if(n==0)
            break;
        if(prise[n]==1&&panduan()==1)//满足条件;
        {
            printf("The number %d is a Carmichael number.\n",n);
        }
        else
            printf("%d is normal.\n",n);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值