Carmichael Numbers

2024-04-11 15:32
文章标签 numbers carmichael

本文主要是介绍Carmichael Numbers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

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);}elseprintf("%d is normal.\n",n);}return 0;
}

 

这篇关于Carmichael Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/894467

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

【codeforces】55D. Beautiful numbers 数位DP

传送门:【codeforces】55D. Beautiful numbers 题目分析:被每一位整除则用二进制记录已经包括的数字的个数,以及对2520取模后的状态。由于对5整除当且仅当最后一个数为0或5,对2整除当且仅当最后一个数为偶数,且1~9的最小公倍数为2520,不包括2,5后的最小公倍数为252,所以除最后一层对2520取模,其余时候都对252取模即可。由于整除的状态有限,最多只有

【SGU】113. Nearly prime numbers 合数分解

传送门:【SGU】113. Nearly prime numbers 题目分析:O(sqrt(N))。。 代码如下: #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std ;#define rep( i , a , b ) for

USACO Section 3.1 Humble Numbers

题意: 已知一个集合S  由S的任意子集作为因子  可构造出一个数字  求  这些构造出的数字中第k大的数字是多少 思路: 拿到这题就被“数字不是很多而且比较连续暴力枚举就好”这个思路迷惑了  果断TLE… 跪了一次后想到通过bfs构造可取  这时用了queue维护bfs  用priority_queue维护答案(大顶堆  内部最多k个数字)  用set判重复(5*2=2*5)  写

Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5 Example 2: Inpu

JavaScript - First step - Numbers and operators

Types of numbers Integers 整数Floating point numbers 单精度浮点数Doubles 双精度浮点数Binary 二进制Octal 八进制Hexadecimal 十六进制 Arithmetic operators 算术运算符 + 加法- 减法* 乘法/ 除法% 求余** 指数 (次方 5 ** 5 = 5 * 5 * 5 * 5 * 5) Oper

leetcode 902. Numbers At Most N Given Digit Set

题目链接 Given an array of digits which is sorted in non-decreasing order. You can write numbers using each digits[i] as many times as we want. For example, if digits = ['1','3','5'], we may write number