Check if two numbers are co-prime or not
Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.
Examples :
Input : 2 3
Output : Co-Prime
Input : 4 8
Output : Not Co-Prime
C++
#include<bits/stdc++.h>
using namespace std;
void coprime( int a, int b) {
if ( __gcd(a, b) == 1)
cout << "Co-Prime" << endl;
else
cout << "Not Co-Prime" << endl;
}
int main()
{
int a = 5, b = 6;
coprime(a, b);
a = 8, b = 16;
coprime(a, b);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int __gcd( int a, int b)
{
if (a == 0 || b == 0 )
return 0 ;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static void coprime( int a, int b) {
if ( __gcd(a, b) == 1 )
System.out.println( "Co-Prime" );
else
System.out.println( "Not Co-Prime" );
}
public static void main (String[] args)
{
int a = 5 , b = 6 ;
coprime(a, b);
a = 8 ; b = 16 ;
coprime(a, b);
}
}
|
Python3
def __gcd(a, b):
if (a = = 0 or b = = 0 ): return 0
if (a = = b): return a
if (a > b):
return __gcd(a - b, b)
return __gcd(a, b - a)
def coprime(a, b):
if ( __gcd(a, b) = = 1 ):
print ( "Co-Prime" )
else :
print ( "Not Co-Prime" )
a = 5 ; b = 6
coprime(a, b)
a = 8 ; b = 16
coprime(a, b)
|
C#
using System;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static void coprime( int a, int b) {
if (__gcd(a, b) == 1)
Console.WriteLine( "Co-Prime" );
else
Console.WriteLine( "Not Co-Prime" );
}
public static void Main()
{
int a = 5, b = 6;
coprime(a, b);
a = 8;
b = 16;
coprime(a, b);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0 || $b == 0)
return 0;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function coprime( $a , $b )
{
if (__gcd( $a , $b ) == 1)
echo "Co-Prime" , "\n" ;
else
echo "Not Co-Prime" , "\n" ;
}
$a = 5; $b = 6;
coprime( $a , $b );
$a = 8;
$b = 16;
coprime( $a , $b );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function coprime(a, b)
{
if (__gcd(a, b) == 1)
document.write( "Co-Prime" + "<br>" );
else
document.write( "Not Co-Prime" );
}
var a = 5, b = 6;
coprime(a, b);
a = 8; b = 16;
coprime(a, b);
</script>
|
Output
Co-Prime
Not Co-Prime
Time Complexity: O(log(max(a,b)))
Auxiliary Space: O(1)
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...