Finding a Non Transitive Coprime Triplet in a Range
Given L and R, find a possible non transitive triplet (a, b, c) such that pair (a, b) is coprime and pair (b, c) is coprime but (a, c) is not coprime.
Eg: (2, 5, 6) is a non transitive triplet as pair (2, 5) is coprime and pair (5, 6) is coprime but pair (2, 6) is not coprime
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 also other such triplets present in this range), Here, pair (4, 7) is coprime and pair (7, 8) is coprime but the pair (4, 8) is not coprimeInput : 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 also other such triplets present in this range), Here, pair (23, 25) is coprime and pair (25, 46) is coprime but the pair (23, 46) is not coprime
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 coprime and pair (b, c) is coprime but pair (a, c) isn’t.
C++
// C++ program to find possible non transitive triplets btw L and R #include <bits/stdc++.h> using namespace std; // Function to return gcd of a and b int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // function to check for gcd bool coprime(int a, int b) { // a and b are coprime if their gcd is 1. return (gcd(a, b) == 1); } /* Checks if any possible triplet (a, b, c) satifying the condition that (a, b) is coprime, (b, c) is coprime but (a, c) isnt */void possibleTripletInRange(int L, int R) { bool flag = false; int possibleA, possibleB, possibleC; // Generate and check for all possible triplets // between L and R for (int a = L; a <= R; a++) { for (int b = a + 1; b <= R; b++) { for (int c = b + 1; c <= R; c++) { // if we find any such triplets set flag to true if (coprime(a, b) && coprime(b, c) && !coprime(a, c)) { flag = true; possibleA = a; possibleB = b; possibleC = c; break; } } } } // flag = True indicates that a pair exists // between L and R 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"; } } // Driver code int main() { int L, R; // finding possible Triplet between 2 and 10 L = 2; R = 10; possibleTripletInRange(L, R); // finding possible Triplet between 23 and 46 L = 23; R = 46; possibleTripletInRange(L, R); return 0; } |
Java
// Java program to find possible non // transitive triplets btw L and R class GFG { // Function to return gcd of a and b static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // function to check for gcd static boolean coprime(int a, int b) { // a and b are coprime if their // gcd is 1. return (gcd(a, b) == 1); } // Checks if any possible triplet // (a, b, c) satifying the condition // that (a, b) is coprime, (b, c) is // coprime but (a, c) isnt */ static void possibleTripletInRange(int L, int R) { boolean flag = false; int possibleA = 0, possibleB = 0, possibleC = 0; // Generate and check for all possible // triplets between L and R for (int a = L; a <= R; a++) { for (int b = a + 1; b <= R; b++) { for (int c = b + 1; c <= R; c++) { // if we find any such triplets // set flag to true if (coprime(a, b) && coprime(b, c) && !coprime(a, c)) { flag = true; possibleA = a; possibleB = b; possibleC = c; break; } } } } // flag = True indicates that a pair exists // between L and R 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); } } // Driver code public static void main(String[] args) { int L, R; // finding possible Triplet between // 2 and 10 L = 2; R = 10; possibleTripletInRange(L, R); // finding possible Triplet between // 23 and 46 L = 23; R = 46; possibleTripletInRange(L, R); } } // This code is contributed by // Smitha DInesh Semwal |
Python3
# Python3 program to find possible non # transitive triplets btw L and R # Function to return gcd of a and b def gcd(a, b): if (a == 0): return b; return gcd(b % a, a); # function to check for gcd def coprime(a, b): # a and b are coprime if # their gcd is 1. return (gcd(a, b) == 1); # Checks if any possible triplet # (a, b, c) satifying the condition # that (a, b) is coprime, (b, c) # is coprime but (a, c) isnt def possibleTripletInRange(L, R): flag = False; possibleA = 0; possibleB = 0; possibleC = 0; # Generate and check for all # possible triplets between L and R for a in range(L, R + 1): for b in range(a + 1, R + 1): for c in range(b + 1, R + 1): # if we find any such triplets # set flag to true if (coprime(a, b) and coprime(b, c) and coprime(a, c) == False): flag = True; possibleA = a; possibleB = b; possibleC = c; break; # flag = True indicates that a # pair exists between L and R 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); # Driver Code # finding possible Triplet # between 2 and 10 L = 2; R = 10; possibleTripletInRange(L, R); # finding possible Triplet # between 23 and 46 L = 23; R = 46; possibleTripletInRange(L, R); # This code is contributed by mits |
C#
// C# program to find possible // non transitive triplets // btw L and R using System; class GFG { // Function to return // gcd of a and b static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // function to // check for gcd static bool coprime(int a, int b) { // a and b are coprime // if their gcd is 1. return (gcd(a, b) == 1); } // Checks if any possible // triplet (a, b, c) satifying // the condition that (a, b) // is coprime, (b, c) is // coprime but (a, c) isnt */ static void possibleTripletInRange(int L, int R) { bool flag = false; int possibleA = 0, possibleB = 0, possibleC = 0; // Generate and check for // all possible triplets // between L and R for (int a = L; a <= R; a++) { for (int b = a + 1; b <= R; b++) { for (int c = b + 1; c <= R; c++) { // if we find any // such triplets // set flag to true if (coprime(a, b) && coprime(b, c) && !coprime(a, c)) { flag = true; possibleA = a; possibleB = b; possibleC = c; break; } } } } // flag = True indicates // that a pair exists // between L and R 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); } } // Driver code public static void Main() { int L, R; // finding possible // Triplet between // 2 and 10 L = 2; R = 10; possibleTripletInRange(L, R); // finding possible // Triplet between // 23 and 46 L = 23; R = 46; possibleTripletInRange(L, R); } } // This code is contributed // by anuj_67. |
PHP
<?php // PHP program to find possible non // transitive triplets btw L and R // Function to return gcd of a and b function gcd($a, $b) { if ($a == 0) return $b; return gcd($b % $a, $a); } // function to check for gcd function coprime($a, $b) { // a and b are coprime if // their gcd is 1. return (gcd($a, $b) == 1); } /* Checks if any possible triplet (a, b, c) satifying the condition that (a, b) is coprime, (b, c) is coprime but (a, c) isnt */function possibleTripletInRange($L, $R) { $flag = false; $possibleA; $possibleB; $possibleC; // Generate and check for all // possible triplets between L and R for ($a = $L; $a <= $R; $a++) { for ($b = $a + 1; $b <= $R; $b++) { for ( $c = $b + 1; $c <= $R; $c++) { // if we find any such triplets // set flag to true if (coprime($a, $b) && coprime($b, $c) && !coprime($a, $c)) { $flag = true; $possibleA = $a; $possibleB = $b; $possibleC = $c; break; } } } } // flag = True indicates that a // pair exists between L and R 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"; } } // Driver Code $L; $R; // finding possible Triplet // between 2 and 10 $L = 2; $R = 10; possibleTripletInRange($L, $R); // finding possible Triplet // between 23 and 46 $L = 23; $R = 46; possibleTripletInRange($L, $R); // This code is contributed by jit_t ?> |
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
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 breakdown 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 cant 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,
Its a well known proof that consecutive numbers are always coprime. 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 coprime 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 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 pair of consecutive numbers are coprime pairs so we need to check if the pair (2k – 1, 2k + 1) is coprime or not
It can be proved that the pair (2k – 1, 2k + 1) is always coprime 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 divisble 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 wont be able to find any possible valid triplet.
Below is the implementation of above approach:
C++
/* C++ program to find a non transitive co-prime triplets between L and R */#include <bits/stdc++.h> using namespace std; /* Checks if any possible triplet (a, b, c) satifying the condition that (a, b) is coprime, (b, c) is coprime but (a, c) isnt */ void possibleTripletInRange(int L, int R) { bool flag = false; int possibleA, possibleB, possibleC; int numbersInRange = (R - L + 1); /* Case 1 : Less than 3 numbers between L and R */ if (numbersInRange < 3) { flag = false; } /* Case 2: More than 3 numbers between L and R */ else if (numbersInRange > 3) { flag = true; // triplets should always be of form (2k, 2k + 1, 2k + 2) if (L % 2) { L++; } possibleA = L; possibleB = L + 1; possibleC = L + 2; } else { /* Case 3.1: Exactly 3 numbers in range of form (2k, 2k + 1, 2k + 2) */ if (!(L % 2)) { flag = true; possibleA = L; possibleB = L + 1; possibleC = L + 2; } else { /* Case 3.2: Exactly 3 numbers in range of form (2k - 1, 2k, 2k + 1) */ flag = false; } } // flag = True indicates that a pair exists between L and R 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"; } } // Driver code int main() { int L, R; // finding possible Triplet between 2 and 10 L = 2; R = 10; possibleTripletInRange(L, R); // finding possible Triplet between 23 and 46 L = 23; R = 46; possibleTripletInRange(L, R); return 0; } |
Java
// Java program to find a // non transitive co-prime // triplets between L and R import java.io.*; class GFG { // Checks if any possible triplet // (a, b, c) satifying the condition // that (a, b) is coprime, (b, c) // is coprime but (a, c) isnt static void possibleTripletInRange(int L, int R) { boolean flag = false; int possibleA = 0, possibleB = 0, possibleC = 0; int numbersInRange = (R - L + 1); // Case 1 : Less than 3 // numbers between L and R if (numbersInRange < 3) { flag = false; } // Case 2: More than 3 // numbers between L and R else if (numbersInRange > 3) { flag = true; // triplets should always // be of form (2k, 2k + 1, // 2k + 2) if (L % 2 > 0) { L++; } possibleA = L; possibleB = L + 1; possibleC = L + 2; } else { /* Case 3.1: Exactly 3 numbers in range of form (2k, 2k + 1, 2k + 2) */ if (!(L % 2 > 0)) { flag = true; possibleA = L; possibleB = L + 1; possibleC = L + 2; } else { /* Case 3.2: Exactly 3 numbers in range of form (2k - 1, 2k, 2k + 1) */ flag = false; } } // flag = True indicates // that a pair exists // between L and R 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); } } // Driver code public static void main (String[] args) { int L, R; // finding possible Triplet // between 2 and 10 L = 2; R = 10; possibleTripletInRange(L, R); // finding possible Triplet // between 23 and 46 L = 23; R = 46; possibleTripletInRange(L, R); } } // This code is contributed // by anuj_67. |
Python3
# Python3 program to find a non transitive # co-prime triplets between L and R # Checks if any possible triplet (a, b, c) # satifying the condition that (a, b) is # coprime, (b, c) is coprime but (a, c) isnt def possibleTripletInRange(L, R): flag = False; possibleA = 0; possibleB = 0; possibleC = 0; numbersInRange = (R - L + 1); # Case 1 : Less than 3 numbers # between L and R if (numbersInRange < 3): flag = False; # Case 2: More than 3 numbers # between L and R elif (numbersInRange > 3): flag = True; # triplets should always be of # form (2k, 2k + 1, 2k + 2) if ((L % 2) > 0): L += 1; possibleA = L; possibleB = L + 1; possibleC = L + 2; else: # Case 3.1: Exactly 3 numbers in range # of form (2k, 2k + 1, 2k + 2) if ((L % 2) == 0): flag = True; possibleA = L; possibleB = L + 1; possibleC = L + 2; else: # Case 3.2: Exactly 3 numbers in range # of form (2k - 1, 2k, 2k + 1) flag = False; # flag = True indicates that a pair # exists between L and R 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); # Driver code # finding possible Triplet # between 2 and 10 L = 2; R = 10; possibleTripletInRange(L, R); # finding possible Triplet # between 23 and 46 L = 23; R = 46; possibleTripletInRange(L, R); # This code is contributed by mits |
C#
// C# program to find a // non transitive co-prime // triplets between L and R using System; public class GFG{ // Checks if any possible triplet // (a, b, c) satifying the condition // that (a, b) is coprime, (b, c) // is coprime but (a, c) isnt static void possibleTripletInRange(int L, int R) { bool flag = false; int possibleA = 0, possibleB = 0, possibleC = 0; int numbersInRange = (R - L + 1); // Case 1 : Less than 3 // numbers between L and R if (numbersInRange < 3) { flag = false; } // Case 2: More than 3 // numbers between L and R else if (numbersInRange > 3) { flag = true; // triplets should always // be of form (2k, 2k + 1, // 2k + 2) if (L % 2 > 0) { L++; } possibleA = L; possibleB = L + 1; possibleC = L + 2; } else { /* Case 3.1: Exactly 3 numbers in range of form (2k, 2k + 1, 2k + 2) */ if (!(L % 2 > 0)) { flag = true; possibleA = L; possibleB = L + 1; possibleC = L + 2; } else { /* Case 3.2: Exactly 3 numbers in range of form (2k - 1, 2k, 2k + 1) */ flag = false; } } // flag = True indicates // that a pair exists // between L and R 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); } } // Driver code static public void Main (){ int L, R; // finding possible Triplet // between 2 and 10 L = 2; R = 10; possibleTripletInRange(L, R); // finding possible Triplet // between 23 and 46 L = 23; R = 46; possibleTripletInRange(L, R); } } // This code is contributed by ajit |
PHP
<?php // PHP program to find possible // non transitive triplets // btw L and R function gcd($a, $b) { if ($a == 0) return $b; return gcd($b % $a, $a); } // function to check for gcd function coprime($a, $b) { // a and b are coprime // if their gcd is 1. return (gcd($a, $b) == 1); } /* Checks if any possible triplet (a, b, c) satifying the condition that (a, b) is coprime, (b, c) is coprime but (a, c) isnt */function possibleTripletInRange($L, $R) { $flag = false; $possibleA; $possibleB; $possibleC; // Generate and check for // all possible triplets // between L and R for ($a = $L; $a <= $R; $a++) { for ($b = $a + 1; $b <= $R; $b++) { for ($c = $b + 1; $c <= $R; $c++) { // if we find any such // triplets set flag to true if (coprime($a, $b) && coprime($b, $c) && !coprime($a, $c)) { $flag = true; $possibleA = $a; $possibleB = $b; $possibleC = $c; break; } } } } // flag = True indicates // that a pair exists // between L and R 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"; } } // Driver code // finding possible Triplet // between 2 and 10 $L = 2; $R = 10; possibleTripletInRange($L, $R); // finding possible Triplet // between 23 and 46 $L = 23; $R = 46; possibleTripletInRange($L, $R); // This code is contributed // by Akanksha Rai(Abby_akku) |
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
Time complexity of this method is O(1).
Recommended Posts:
- Composite XOR and Coprime AND
- Largest number less than or equal to N/2 which is coprime to N
- Partition first N natural number into two sets such that their sum is not coprime
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Prime Triplet
- Pythagorean Triplet with given sum
- Triplet with no element divisible by 3 and sum N
- Sum of multiplication of triplet of divisors of a number
- Find a triplet in an array whose sum is closest to a given number
- Generate a pythagoras triplet from a single integer
- Binary Indexed Tree : Range Update and Range Queries
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Range and Coefficient of range of Array
- Finding n-th term of series 3, 13, 42, 108, 235…
- Finding all subsets of a given set in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



