📌  相关文章
📜  在一个范围内找到一个非传递性互质三元组

📅  最后修改于: 2021-04-26 10:37:04             🧑  作者: Mango

给定L和R,找到一个可能的非传递三元组(a,b,c),使得对(a,b)是互质的,而对(b,c)是互质的,而(a,c)不是互质的。
例如:(2,5,6)是不可传递的三元组,因为对(2,5)是互质的,而对(5,6)是互质的,而对(2,6)不是互质的

例子:

方法1(蛮力):

我们在L和R之间生成所有可能的三元组,并检查该属性是否成立,对(a,b)是互素的,而对(b,c)是互素的,而对(a,c)则不是。

C++
// C++ program to find possible non transitive triplets btw L and R
#include 
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


C++
/* C++ program to find a non transitive co-prime
   triplets between L and R */
#include 
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


输出:

(8, 9, 10) is one such possible triplet between 2 and 10
(44, 45, 46) is one such possible triplet between 23 and 46

蛮力解的时间复杂度为O(n 3 log(A)),其中A是三元组的最小数目。
注意:复杂度的对数因子是针对一对数字计算GCD的对数因子。

方法2(有效)

由于我们仅需要一对这样的可能对,因此我们可以使用它来进一步分解我们的复杂性。

我们只需要确定一些情况并寻求解决这些问题即可解决此问题。

情况1:L和R之间的数字少于3。
这种情况很简单,我们不能形成任何三元组,所以答案是这种情况永远是“不可能”

情况2:L和R之间有三个以上的数字。
现在,
这是众所周知的证明,连续数字始终是互质的。我们甚至可以轻松证明这一点。

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.

因此,如果我们采用三个连续的2k,2k + 1、2k + 2形式的数字,那么我们最终总是会得到可能的三元组,因为如上所述,对(2k,2k + 1)和(2k + 1,2k + 2)连续数对是互质数,而对(2k,2k + 2)的gcd为2(因为它们是偶数)。

情况3:当L和R之间恰好有3个数字时
这是案例3的扩展,现在这个案例可以有2个案例,

情况3.1当三个数字的格式为2k,2k + 1,2k + 2
我们已经在案例2中研究了这种情况。因此,这是唯一的三元组,也是L和R之间的有效三元组。

情况3.2当三个数字的形式为2k – 1,2k,2k + 1
我们已经看到,作为连续数字对的(2k – 1,2k)和(2k,2k + 1)是互质对,因此我们需要检查(2k – 1,2k + 1)对是否互质。
可以证明,对(2k – 1,2k + 1)始终是互质的,如下所示

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.

因此,在这种情况下,我们将无法找到任何可能的有效三元组。

下面是上述方法的实现:

C++

/* C++ program to find a non transitive co-prime
   triplets between L and R */
#include 
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

输出:

(2, 3, 4) is one such possible triplet between 2 and 10
(24, 25, 26) is one such possible triplet between 24 and 46

该方法的时间复杂度为O(1)。