UVA 10006 10006-carmichael Numbers

Source: Internet
Author: User
Carmichael Numbers
An important topic nowadays in computer science is cryptography. Some people even think that cryptography are the only important field in computer science, and that life would not matter a T all without cryptography. Alvaro is one of the such persons, and is designing a set of cryptographic procedures for cooking paella. Some of the cryptographic algorithms he is implementing make use of the big prime numbers. However, checking if a big number is prime isn't so easy. An exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square ro Ot. For big numbers, the amount of time and storage needed for such operations would certainly ruin the paella.

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

Let A is 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:

\begin{displaymath}a^n \bmod n = a\end{displaymath}

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

Unfortunately, there is bad news. Some numbers that is not prime still pass the Fermat test with every number smaller than themselves. These numbers is called Carmichael numbers.

In this problem you asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task'll one day is able to taste a delicious portion of encrypted paella. As a side note, we need to mention so, according to Alvaro, the main advantage of encrypted paella over conventional PAE Lla is the nobody but you knows and what's eating.

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

Output
For each of the number in the input, you had to print if it was a Carmichael number or not and as shown in the sample output.

Sample Input

1729
17
561
1109
431
0

Sample Output

The number 1729 is a Carmichael number.
is normal.
The number 561 is a Carmichael number.
1109 is normal.
431 is normal.


With the quick power template, if all number a in 2~~n-1 is satisfied with a^n mod n = A, then n is a Mitchell number

At first, it was wrong to think that as long as there is a 2~~n-1 in the relationship between the Mitchell number, submitted many times have expired, changed after AC.

#include <iostream> #include <cstdio> using namespace std;
    BOOL Is_prime (int n) {for (int i=2;i*i<=n;i++) if (n%i==0) return false;
return true;
    } int Quickpow (int a, int b, int c) {//calculation, good for large number calculation pow (A, b)%c long long int ans = 1;
    a=a%c;
          while (b > 0) {if (b & 1) ans = (ans*a)%c;
          b = b >> 1;
    A = ((Long long int) a*a)%c;
} return ans;
    } int main (void) {int n;
        while (scanf ("%d", &n)!=eof&&n) {if (Is_prime (n)) printf ("%d is normal.\n", N);
            else {int ok=0; for (int i=2;i<=n-1;i++) {if (I!=quickpow (i,n,n)) {ok=1
                    ;
                    printf ("%d is normal.\n", N);
                Break

        }} if (ok==0) printf ("The number%d is a Carmichael number.\n", N); }
    }

    return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.