Pseudoprime numbers

来源:互联网 发布:微信攻击软件 编辑:程序博客网 时间:2024/06/16 17:20

Description

Fermat's theorem states that for any prime number p andfor any integer a > 1, ap == a (mod p).That is, if we raise a to the pth power and divide byp, the remainder is a. Some (but not very many)non-prime values of p, known as base-a pseudoprimes,have this property for some a. (And some, known asCarmichael Numbers, are base-a pseudoprimes for all a.)

Given 2 < p ≤ 1,000,000,000 and 1 < a <p, determine whether or not p is a base-apseudoprime.

Input contains several test cases followed by a line containing"0 0". Each test case consists of a line containing p anda. For each test case, output "yes" if p is a base-apseudoprime; otherwise output "no".

Input

Output

Sample Input

3 2 
10 3 
341 2 
341 3 
11052 
1105 3 
0 0

Sample Output

no 
no 
yes
no 
yes 
yes


  1. #include<stdio.h>
  2. #include<math.h>
  3. int main()
  4. {
  5.    long longa,p;
  6.    int isprime(longlong n);
  7.    long longmodular(long longa,long longr,long longm);
  8.    while(scanf("%lld%lld",&p,&a)!=EOF)
  9.    {
  10.       if(p==0&&a==0)
  11.           break;
  12.       long longresult;
  13.       if(isprime(p))
  14.           printf("no\n");
  15.       else
  16.       {
  17.           if(a==modular(a,p,p))
  18.   printf("yes\n");
  19.           else
  20.               printf("no\n");
  21.       }
  22.    }
  23.    return 0;
  24. }
  25. int isprime(longlong n)
  26. {
  27.    if(n==2)
  28.       return 1;
  29.    if(n<=1||n%2==0)
  30.       return 0;
  31.    long longj=3;
  32.    while(j<=(longlong)sqrt(double(n)))
  33.    {
  34.       if(n%j==0)
  35.           return 0;
  36.       j+=2;
  37.    }
  38.    return 1;
  39. }
  40. long long modular(longlong a,longlong r,longlong m)
  41. {
  42.    long longd=1,t=a;
  43.    while(r>0)
  44.    {
  45.       if(r%2==1)
  46.           d=(d*t)%m;
  47.       r/=2;
  48.       t=t*t%m;
  49.    }
  50.    return d;
  51.  
0 0
原创粉丝点击