如果A和B的最大公约数为1,则称它们为共素数或互素数。给你两个数字A和B,看看它们是否是共质数。 例如:
null
Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-Prime
C++
// CPP program to check if two // numbers are co-prime or not #include<bits/stdc++.h> using namespace std; // function to check and print if // two numbers are co-prime or not void coprime( int a, int b) { if ( __gcd(a, b) == 1) cout << "Co-Prime" << endl; else cout << "Not Co-Prime" << endl; } // driver code int main() { int a = 5, b = 6; coprime(a, b); a = 8, b = 16; coprime(a, b); return 0; } |
JAVA
// Java program to check if two // numbers are co-prime or not class GFG { // Recursive function to // return gcd of a and b static int __gcd( int a, int b) { // Everything divides 0 if (a == 0 || b == 0 ) return 0 ; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a-b, b); return __gcd(a, b-a); } // function to check and print if // two numbers are co-prime or not static void coprime( int a, int b) { if ( __gcd(a, b) == 1 ) System.out.println( "Co-Prime" ); else System.out.println( "Not Co-Prime" ); } //driver code public static void main (String[] args) { int a = 5 , b = 6 ; coprime(a, b); a = 8 ; b = 16 ; coprime(a, b); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to check if two # numbers are co-prime or not # Recursive function to # return gcd of a and b def __gcd(a, b): # Everything divides 0 if (a = = 0 or b = = 0 ): return 0 # base case if (a = = b): return a # a is greater if (a > b): return __gcd(a - b, b) return __gcd(a, b - a) # Function to check and print if # two numbers are co-prime or not def coprime(a, b): if ( __gcd(a, b) = = 1 ): print ( "Co-Prime" ) else : print ( "Not Co-Prime" ) # Driver code a = 5 ; b = 6 coprime(a, b) a = 8 ; b = 16 coprime(a, b) # This code is contributed by Anant Agarwal |
C#
// C# program to check if two // numbers are co-prime or not using System; class GFG { // Recursive function to // return gcd of a and b static int __gcd( int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // function to check and print if // two numbers are co-prime or not static void coprime( int a, int b) { if (__gcd(a, b) == 1) Console.WriteLine( "Co-Prime" ); else Console.WriteLine( "Not Co-Prime" ); } // Driver code public static void Main() { int a = 5, b = 6; coprime(a, b); a = 8; b = 16; coprime(a, b); } } // This code is contributed by Anant Agarwal. |
PHP
<?php // PHP program to check if two // numbers are co-prime or not // Recursive function to // return gcd of a and b function __gcd( $a , $b ) { // Everything divides 0 if ( $a == 0 || $b == 0) return 0; // base case if ( $a == $b ) return $a ; // a is greater if ( $a > $b ) return __gcd( $a - $b , $b ); return __gcd( $a , $b - $a ); } // function to check and print if // two numbers are co-prime or not function coprime( $a , $b ) { if (__gcd( $a , $b ) == 1) echo "Co-Prime" , "" ; else echo "Not Co-Prime" , "" ; } // Driver Code $a = 5; $b = 6; coprime( $a , $b ); $a = 8; $b = 16; coprime( $a , $b ); // This code is contributed by aj_36 ?> |
Javascript
<script> // Javascript program to check if two // numbers are co-prime or not // Recursive function to // return gcd of a and b function __gcd(a, b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // Base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to check and print if // two numbers are co-prime or not function coprime(a, b) { if (__gcd(a, b) == 1) document.write( "Co-Prime" + "<br>" ); else document.write( "Not Co-Prime" ); } // Driver Code var a = 5, b = 6; coprime(a, b); a = 8; b = 16; coprime(a, b); // This code is contributed by Kirti </script> |
输出:
Co-PrimeNot Co-Prime
本文由 迪比恩杜·罗伊·乔杜里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END