Finding a Non Transitive Co-prime Triplet in a Range
Given L and R, find a possible non-transitive triplet (a, b, c) such that pair (a, b) is co-prime and pair (b, c) is co-prime but (a, c) is not co-prime.
Eg: (2, 5, 6) is a non-transitive triplet as pair (2, 5) is co-prime and pair (5, 6) is co-prime but pair (2, 6) is not co-prime.
Examples:
Input : L = 2, R = 10
Output : a = 4, b = 7, c = 8 is one such triplet
Explanation (4, 7, 8) is a possible triplet (while there are other such triplets present in this range), Here, pair (4, 7) is co-prime and pair (7, 8) is co-prime but the pair (4, 8) is not co-prime
Input : L = 21, R = 47
Output : a = 23, b = 25, c = 46 is one such triplet
Explanation (23, 25, 46) is a possible triplet (while there are other such triplets present in this range), Here, pair (23, 25) is co-prime and pair (25, 46) is co-prime but the pair (23, 46) is not co-prime
Method 1 (Brute Force) :
We generate all possible Triplets between L and R and check if the property holds true that pair (a, b) is co-prime and pair (b, c) is co-prime but pair (a, c) isn’t.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
bool coprime( int a, int b)
{
return (gcd(a, b) == 1);
}
void possibleTripletInRange( int L, int R)
{
bool flag = false ;
int possibleA, possibleB, possibleC;
for ( int a = L; a <= R; a++) {
for ( int b = a + 1; b <= R; b++) {
for ( int c = b + 1; c <= R; c++) {
if (coprime(a, b) && coprime(b, c) && !coprime(a, c)) {
flag = true ;
possibleA = a;
possibleB = b;
possibleC = c;
break ;
}
}
}
}
if (flag == true ) {
cout << "(" << possibleA << ", " << possibleB
<< ", " << possibleC << ")"
<< " is one such possible triplet between "
<< L << " and " << R << "\n" ;
}
else {
cout << "No Such Triplet exists between "
<< L << " and " << R << "\n" ;
}
}
int main()
{
int L, R;
L = 2;
R = 10;
possibleTripletInRange(L, R);
L = 23;
R = 46;
possibleTripletInRange(L, R);
return 0;
}
|
Java
class GFG {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static boolean coprime( int a, int b)
{
return (gcd(a, b) == 1 );
}
static void possibleTripletInRange( int L, int R)
{
boolean flag = false ;
int possibleA = 0 , possibleB = 0 ,
possibleC = 0 ;
for ( int a = L; a <= R; a++) {
for ( int b = a + 1 ; b <= R; b++) {
for ( int c = b + 1 ; c <= R; c++)
{
if (coprime(a, b) && coprime(b, c)
&& !coprime(a, c))
{
flag = true ;
possibleA = a;
possibleB = b;
possibleC = c;
break ;
}
}
}
}
if (flag == true ) {
System.out.println( "(" + possibleA + ", "
+ possibleB + ", " + possibleC + ")"
+ " is one such possible triplet "
+ "between " + L + " and " + R);
}
else {
System.out.println( "No Such Triplet exists"
+ "between " + L + " and " + R);
}
}
public static void main(String[] args)
{
int L, R;
L = 2 ;
R = 10 ;
possibleTripletInRange(L, R);
L = 23 ;
R = 46 ;
possibleTripletInRange(L, R);
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b;
return gcd(b % a, a);
def coprime(a, b):
return (gcd(a, b) = = 1 );
def possibleTripletInRange(L, R):
flag = False ;
possibleA = 0 ;
possibleB = 0 ;
possibleC = 0 ;
for a in range (L, R + 1 ):
for b in range (a + 1 , R + 1 ):
for c in range (b + 1 , R + 1 ):
if (coprime(a, b) and coprime(b, c) and
coprime(a, c) = = False ):
flag = True ;
possibleA = a;
possibleB = b;
possibleC = c;
break ;
if (flag = = True ):
print ( "(" , possibleA, "," , possibleB,
"," , possibleC, ") is one such" ,
"possible triplet between" , L, "and" , R);
else :
print ( "No Such Triplet exists between" ,
L, "and" , R);
L = 2 ;
R = 10 ;
possibleTripletInRange(L, R);
L = 23 ;
R = 46 ;
possibleTripletInRange(L, R);
|
C#
using System;
class GFG
{
static int gcd( int a,
int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static bool coprime( int a,
int b)
{
return (gcd(a, b) == 1);
}
static void possibleTripletInRange( int L,
int R)
{
bool flag = false ;
int possibleA = 0,
possibleB = 0,
possibleC = 0;
for ( int a = L; a <= R; a++)
{
for ( int b = a + 1;
b <= R; b++)
{
for ( int c = b + 1;
c <= R; c++)
{
if (coprime(a, b) &&
coprime(b, c) &&
!coprime(a, c))
{
flag = true ;
possibleA = a;
possibleB = b;
possibleC = c;
break ;
}
}
}
}
if (flag == true )
{
Console.WriteLine( "(" + possibleA + ", " +
possibleB + ", " +
possibleC + ")" +
" is one such possible triplet " +
"between " + L + " and " + R);
}
else
{
Console.WriteLine( "No Such Triplet exists" +
"between " + L + " and " + R);
}
}
public static void Main()
{
int L, R;
L = 2;
R = 10;
possibleTripletInRange(L, R);
L = 23;
R = 46;
possibleTripletInRange(L, R);
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function coprime( $a , $b )
{
return (gcd( $a , $b ) == 1);
}
function possibleTripletInRange( $L , $R )
{
$flag = false;
$possibleA ;
$possibleB ;
$possibleC ;
for ( $a = $L ; $a <= $R ; $a ++)
{
for ( $b = $a + 1; $b <= $R ; $b ++)
{
for ( $c = $b + 1; $c <= $R ; $c ++)
{
if (coprime( $a , $b ) &&
coprime( $b , $c ) &&
!coprime( $a , $c ))
{
$flag = true;
$possibleA = $a ;
$possibleB = $b ;
$possibleC = $c ;
break ;
}
}
}
}
if ( $flag == true)
{
echo "(" , $possibleA ,
", " , $possibleB ,
", " , $possibleC , ")" ,
" is one such possible triplet between " ,
$L , " and " , $R , "\n" ;
}
else
{
echo "No Such Triplet exists between " ,
$L , " and " , $R , "\n" ;
}
}
$L ;
$R ;
$L = 2;
$R = 10;
possibleTripletInRange( $L , $R );
$L = 23;
$R = 46;
possibleTripletInRange( $L , $R );
?>
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function coprime(a, b)
{
return (gcd(a, b) == 1);
}
function possibleTripletInRange(L, R)
{
let flag = false ;
let possibleA = 0,
possibleB = 0,
possibleC = 0;
for (let a = L; a <= R; a++)
{
for (let b = a + 1;
b <= R; b++)
{
for (let c = b + 1;
c <= R; c++)
{
if (coprime(a, b) &&
coprime(b, c) &&
!coprime(a, c))
{
flag = true ;
possibleA = a;
possibleB = b;
possibleC = c;
break ;
}
}
}
}
if (flag == true )
{
document.write( "(" + possibleA + ", " +
possibleB + ", " +
possibleC + ")" +
" is one such possible triplet " +
"between " + L + " and " + R + "</br>" );
}
else
{
document.write( "No Such Triplet exists" +
"between " + L + " and " + R + "</br>" );
}
}
let L, R;
L = 2;
R = 10;
possibleTripletInRange(L, R);
L = 23;
R = 46;
possibleTripletInRange(L, R);
</script>
|
Output:
(8, 9, 10) is one such possible triplet between 2 and 10
(44, 45, 46) is one such possible triplet between 23 and 46
The time Complexity of the Brute Force Solution is O(n3log(A)) where A is the smallest number of the triplet.
Note: The log factor of the complexity is that of computing the GCD for a pair of numbers.
Method 2 (efficient):
Since we need only one such possible pair, we can use this to break down our complexity further.
We just need to identify some cases and look to solve those to solve this problem.
Case 1: There are less than 3 numbers between L and R.
This Case is easy, we can’t form any triplets, so the answer is this case would always be ‘Not Possible’
Case 2: There are more than three numbers between L and R.
Now,
It’s well-known proof that consecutive numbers are always co-prime. We can even prove this easily.
Proof:
Given that N and N + 1 are two consecutive integers.
Now suppose gcd(n, n + 1) = X,
? X divides n and X also divides (n + 1).
Which implies that X divides ((n + 1) - n) or X divides 1.
But, There is no number which divides 1 except 1.
? X = 1, or we can also say that gcd(n, n + 1) = 1
Thus, n and n + 1 are coprime.
So, if we take three consecutive numbers of the form 2k, 2k + 1, 2k + 2 we would always end up having a possible triplet because as proved above, pairs (2k, 2k + 1) and (2k + 1, 2k + 2) being pairs of consecutive numbers are co-prime and the pair (2k, 2k+2) have their gcd as 2 (since they are even).
Case 3: When there are exactly 3 numbers between L and R
This is an extension of case 3, now this case can have 2 cases,
Case 3.1 When the three numbers are of the form 2k, 2k + 1, 2k + 2
We have already looked at this case in case 2. So this is the only triplet and also is a valid triplet between L and R.
Case 3.2 When the three numbers are of the form 2k – 1, 2k, 2k + 1
We have already seen that (2k – 1, 2k) and (2k, 2k + 1) being a pair of consecutive numbers are co-prime pairs, so we need to check if the pair (2k – 1, 2k + 1) is co-prime or not
It can be proved that the pair (2k – 1, 2k + 1) is always co-prime as shown below
Proof:
Given that 2k - 1 and 2k + 1 are two numbers
Now suppose gcd((2k - 1), (2k + 1)) = X,
? X divides (2k - 1) and X also divides (2k + 1).
Which implies that X divides ((2k + 1) - (2k - 1)) or X divides 2.
2 being a prime is only divisible by 1 and 2 itself.
But, 2k - 1 and 2k + 1 are odd numbers so X can never be equal to 2.
? X = 1, or we can also say that gcd((2k -1), (2k + 1)) = 1
Thus, 2k - 1 and 2k + 1 are coprime.
Thus, in this case, we won’t be able to find any possible valid triplet.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void possibleTripletInRange( int L, int R)
{
bool flag = false ;
int possibleA, possibleB, possibleC;
int numbersInRange = (R - L + 1);
if (numbersInRange < 3) {
flag = false ;
}
else if (numbersInRange > 3) {
flag = true ;
if (L % 2) {
L++;
}
possibleA = L;
possibleB = L + 1;
possibleC = L + 2;
}
else {
if (!(L % 2)) {
flag = true ;
possibleA = L;
possibleB = L + 1;
possibleC = L + 2;
}
else {
flag = false ;
}
}
if (flag == true ) {
cout << "(" << possibleA << ", " << possibleB
<< ", " << possibleC << ")"
<< " is one such possible triplet between "
<< L << " and " << R << "\n" ;
}
else {
cout << "No Such Triplet exists between "
<< L << " and " << R << "\n" ;
}
}
int main()
{
int L, R;
L = 2;
R = 10;
possibleTripletInRange(L, R);
L = 23;
R = 46;
possibleTripletInRange(L, R);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void possibleTripletInRange( int L,
int R)
{
boolean flag = false ;
int possibleA = 0 ,
possibleB = 0 ,
possibleC = 0 ;
int numbersInRange = (R - L + 1 );
if (numbersInRange < 3 )
{
flag = false ;
}
else if (numbersInRange > 3 )
{
flag = true ;
if (L % 2 > 0 )
{
L++;
}
possibleA = L;
possibleB = L + 1 ;
possibleC = L + 2 ;
}
else
{
if (!(L % 2 > 0 ))
{
flag = true ;
possibleA = L;
possibleB = L + 1 ;
possibleC = L + 2 ;
}
else
{
flag = false ;
}
}
if (flag == true )
{
System.out.println( "(" + possibleA +
", " + possibleB +
", " + possibleC +
")" + " is one such possible" +
" triplet between " +
L + " and " + R );
}
else {
System.out.println( "No Such Triplet" +
" exists between " +
L + " and " + R);
}
}
public static void main (String[] args)
{
int L, R;
L = 2 ;
R = 10 ;
possibleTripletInRange(L, R);
L = 23 ;
R = 46 ;
possibleTripletInRange(L, R);
}
}
|
Python3
def possibleTripletInRange(L, R):
flag = False ;
possibleA = 0 ;
possibleB = 0 ;
possibleC = 0 ;
numbersInRange = (R - L + 1 );
if (numbersInRange < 3 ):
flag = False ;
elif (numbersInRange > 3 ):
flag = True ;
if ((L % 2 ) > 0 ):
L + = 1 ;
possibleA = L;
possibleB = L + 1 ;
possibleC = L + 2 ;
else :
if ((L % 2 ) = = 0 ):
flag = True ;
possibleA = L;
possibleB = L + 1 ;
possibleC = L + 2 ;
else :
flag = False ;
if (flag = = True ):
print ( "(" , possibleA, "," , possibleB,
"," , possibleC, ") is one such" ,
"possible triplet between" , L, "and" , R);
else :
print ( "No Such Triplet exists between" ,
L, "and" , R);
L = 2 ;
R = 10 ;
possibleTripletInRange(L, R);
L = 23 ;
R = 46 ;
possibleTripletInRange(L, R);
|
C#
using System;
public class GFG{
static void possibleTripletInRange( int L,
int R)
{
bool flag = false ;
int possibleA = 0,
possibleB = 0,
possibleC = 0;
int numbersInRange = (R - L + 1);
if (numbersInRange < 3)
{
flag = false ;
}
else if (numbersInRange > 3)
{
flag = true ;
if (L % 2 > 0)
{
L++;
}
possibleA = L;
possibleB = L + 1;
possibleC = L + 2;
}
else
{
if (!(L % 2 > 0))
{
flag = true ;
possibleA = L;
possibleB = L + 1;
possibleC = L + 2;
}
else
{
flag = false ;
}
}
if (flag == true )
{
Console.WriteLine( "(" + possibleA +
", " + possibleB +
", " + possibleC +
")" + " is one such possible" +
" triplet between " +
L + " and " + R );
}
else {
Console.WriteLine( "No Such Triplet" +
" exists between " +
L + " and " + R);
}
}
static public void Main (){
int L, R;
L = 2;
R = 10;
possibleTripletInRange(L, R);
L = 23;
R = 46;
possibleTripletInRange(L, R);
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function coprime( $a , $b )
{
return (gcd( $a , $b ) == 1);
}
function possibleTripletInRange( $L , $R )
{
$flag = false;
$possibleA ;
$possibleB ;
$possibleC ;
for ( $a = $L ; $a <= $R ; $a ++)
{
for ( $b = $a + 1; $b <= $R ; $b ++)
{
for ( $c = $b + 1; $c <= $R ; $c ++)
{
if (coprime( $a , $b ) &&
coprime( $b , $c ) &&
!coprime( $a , $c ))
{
$flag = true;
$possibleA = $a ;
$possibleB = $b ;
$possibleC = $c ;
break ;
}
}
}
}
if ( $flag == true)
{
echo "(" , $possibleA ,
", " , $possibleB ,
", " , $possibleC ,
")" , " is one such possible " ,
"triplet between " , $L ,
" and " , $R , "\n" ;
}
else
{
echo "No Such Triplet exists between " ,
$L , " and " , $R , "\n" ;
}
}
$L = 2;
$R = 10;
possibleTripletInRange( $L , $R );
$L = 23;
$R = 46;
possibleTripletInRange( $L , $R );
|
Javascript
<script>
function possibleTripletInRange(L, R)
{
let flag = false ;
let possibleA, possibleB, possibleC;
let numbersInRange = (R - L + 1);
if (numbersInRange < 3) {
flag = false ;
}
else if (numbersInRange > 3) {
flag = true ;
if (L % 2) {
L++;
}
possibleA = L;
possibleB = L + 1;
possibleC = L + 2;
}
else {
if (!(L % 2)) {
flag = true ;
possibleA = L;
possibleB = L + 1;
possibleC = L + 2;
}
else {
flag = false ;
}
}
if (flag == true ) {
document.write( "(" + possibleA + ", " + possibleB
+ ", " + possibleC + ")"
+ " is one such possible triplet between "
+ L + " and " + R + "</br>" );
}
else {
document.write( "No Such Triplet exists between "
+ L + " and " + R + "</br>" );
}
}
let L, R;
L = 2;
R = 10;
possibleTripletInRange(L, R);
L = 23;
R = 46;
possibleTripletInRange(L, R);
</script>
|
Output:
(2, 3, 4) is one such possible triplet between 2 and 10
(24, 25, 26) is one such possible triplet between 24 and 46
The time complexity of this method is O(1).
Last Updated :
05 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...