Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Finding a Non Transitive Co-prime Triplet in a Range

  • Difficulty Level : Expert
  • Last Updated : 05 May, 2021

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:  

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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++




// 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) satisfying 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
?>

Javascript




<script>
 
    // Javascript 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)
    {
   
        let flag = false;
        let possibleA = 0,
            possibleB = 0,
            possibleC = 0;
   
        // Generate and check for
        // all possible triplets
        // between L and R
        for (let a = L; a <= R; a++)
        {
            for (let b = a + 1;
                     b <= R; b++)
            {
                for (let 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)
        {
            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;
   
    // 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);
     
</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++




/* 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) satisfying 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)

Javascript




<script>
    /* Javascript program to find a non transitive co-prime
   triplets between L and R */
    
   /* Checks if any possible triplet (a, b, c) satisfying the condition
   that (a, b) is coprime, (b, c) is coprime but (a, c) isnt */
   
  function possibleTripletInRange(L, R)
  {
 
    let flag = false;
    let possibleA, possibleB, possibleC;
 
    let 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) {
      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;
   
  // 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);
 
</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).
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!