Advertisement
Guest User

carmichael

a guest
Nov 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. // Struktura do przechowywania trojki liczb
  8. typedef struct
  9. {
  10.     long d,x,y;
  11. } value_t;
  12.  
  13.  
  14.  
  15. value_t extendedGCD (long a,long b)
  16. {
  17.   value_t w,tmp;
  18.  
  19.   if (b==0)
  20.   {
  21.       w.d=a;w.x=1;w.y=0;
  22.       return w;
  23.   }
  24.   else
  25.   {
  26.       tmp=extendedGCD(b,a % b);
  27.       w.d=tmp.d;
  28.       w.x=tmp.y;
  29.       w.y=tmp.x-(a/b)*tmp.y;
  30.       return w;
  31.   }
  32. }
  33.  
  34. bool isPrime (long n)
  35. {
  36.     long double sqr=sqrt(n);
  37.    
  38.     for (long i=2;i<=sqr;i++)
  39.     {
  40.         if (n%i == 0)
  41.             return false;
  42.     }
  43.     return true;
  44. }
  45.  
  46.  
  47. long powermod(long a,long b,long n)
  48. {
  49.     if (b==0)
  50.         return 1;
  51.     else
  52.         if ((b % 2) == 0)
  53.             return (powermod(a,b/2,n)*powermod(a,b/2,n)) % n;
  54.         else
  55.             return (a*powermod(a,(b-1)/2,n)*powermod(a,(b-1)/2,n)) %n;
  56. }
  57.  
  58.  
  59.  
  60. bool isCarmichael(long N)
  61. {
  62.   //TODO      
  63.   for(int i = 2; i < N; i++){
  64.       if(extendedGCD(N, i).d != 1) continue;
  65.       if(powermod(i, N-1, N) != 1) return false;
  66.   }
  67.   return !isPrime(N);
  68. }
  69.  
  70.    
  71.  
  72. int main()
  73. {
  74.     for (long N=2;N<=50000;N++)
  75.     {
  76.         if (!isPrime(N))
  77.             if (isCarmichael(N))
  78.                 cout<<N<<endl;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement