判断素数互素等

  1. #include <iostream>  
  2. #include <cmath>  
  3. using namespace std;  
  4. bool is_prime(int n);  
  5. bool is_coprime(int x,int y);  
  6. int main()  
  7. {  
  8.     int n,m;  
  9.     bool a,b;  
  10.     cin>>n>>m;  
  11.     a=is_prime(n);  
  12.     b=is_coprime(n,m);  
  13.     cout<<a<<b;  
  14. }  
  15. bool is_prime(int n)  
  16. {  
  17.     if(n<=1) return 0;  
  18.     int m=floor(sqrt(n)+0.5);  
  19.     for(int i=2;i<=m;i++)  
  20.     {  
  21.         if(n%i==0) return 0;  
  22.     }  
  23.     return 1;  
  24. }  
  25. bool is_coprime(int x,int y)  
  26. {  
  27.     if(x==1 && y==1)  
  28.         return true;  
  29.     else if(x<=0 || y<=0 || x==y)  
  30.         return false;  
  31.     else if(x==1 || y==1)  
  32.         return true;  
  33.     else  
  34.     {  
  35.         int tmp=0;  
  36.           
  37.         while(true)  
  38.         {  
  39.             tmp=x%y;  
  40.             if(tmp==0)  
  41.             {  
  42.                 break;  
  43.             }  
  44.             else  
  45.             {  
  46.                 x=y;  
  47.                 y=tmp;  
  48.             }  
  49.         }  
  50.         if(y==1)          
  51.             return true;  
  52.         else              
  53.             return false;  
  54.     }  
  55. }  

最大公约数函数:

[html]  view plain  copy
  1. int Division(int a,int b)  
  2. {  
  3.     if(a < b){ temp = aa = bb = temp; }   
  4.     while(a%b != 0){ temp = a%b; a = bb = temp; }  
  5.     return b;  
  6. }   
  7. //最小公倍数可以相乘然后除以最大公约数。  

猜你喜欢

转载自blog.csdn.net/hexiquan123/article/details/80634119