📜  查找给定乘积的三元组的不同整数

📅  最后修改于: 2021-06-27 00:06:41             🧑  作者: Mango

给定一个整数X ,任务是找到三个大于1的不同整数,即ABC ,使得(A * B * C)= X。如果不存在这样的三元组,则打印-1
例子:

方法:假设我们有一个三元组(A,B,C) 。注意,为了使它们的乘积等于X ,每个整数必须是X的因数。因此,使用本文讨论的方法,将所有X因子都存储在O(sqrt(X))时间中。
现在最多会有sqrt(X)个因素。接下来,通过运行两个循环,一个选择A ,另一个选择B ,对每个因子进行迭代。现在,如果该三元组有效,即C = X /(A * B) ,其中C也是X的因子。要检查这一点,请将所有因素存储在unordered_set中。如果找到有效的三元组,则打印三元组,否则打印-1
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the required triplets
void findTriplets(int x)
{
    // To store the factors
    vector fact;
    unordered_set factors;
 
    // Find factors in sqrt(x) time
    for (int i = 2; i <= sqrt(x); i++) {
        if (x % i == 0) {
            fact.push_back(i);
            if (x / i != i)
                fact.push_back(x / i);
            factors.insert(i);
            factors.insert(x / i);
        }
    }
 
    bool found = false;
    int k = fact.size();
    for (int i = 0; i < k; i++) {
 
        // Choose a factor
        int a = fact[i];
        for (int j = 0; j < k; j++) {
 
            // Choose another factor
            int b = fact[j];
 
            // These conditions need to be
            // met for a valid triplet
            if ((a != b) && (x % (a * b) == 0)
                && (x / (a * b) != a)
                && (x / (a * b) != b)
                && (x / (a * b) != 1)) {
 
                // Print the valid triplet
                cout << a << " " << b << " "
                     << (x / (a * b));
                found = true;
                break;
            }
        }
 
        // Triplet found
        if (found)
            break;
    }
 
    // Triplet not found
    if (!found)
        cout << "-1";
}
 
// Driver code
int main()
{
    int x = 105;
 
    findTriplets(x);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to find the required triplets
static void findTriplets(int x)
{
    // To store the factors
    Vector fact = new Vector();
    HashSet factors = new HashSet();
 
    // Find factors in Math.sqrt(x) time
    for (int i = 2; i <= Math.sqrt(x); i++)
    {
        if (x % i == 0)
        {
            fact.add(i);
            if (x / i != i)
                fact.add(x / i);
            factors.add(i);
            factors.add(x / i);
        }
    }
 
    boolean found = false;
    int k = fact.size();
    for (int i = 0; i < k; i++)
    {
 
        // Choose a factor
        int a = fact.get(i);
        for (int j = 0; j < k; j++)
        {
 
            // Choose another factor
            int b = fact.get(j);
 
            // These conditions need to be
            // met for a valid triplet
            if ((a != b) && (x % (a * b) == 0)
                && (x / (a * b) != a)
                && (x / (a * b) != b)
                && (x / (a * b) != 1))
            {
 
                // Print the valid triplet
                System.out.print(a+ " " + b + " "
                    + (x / (a * b)));
                found = true;
                break;
            }
        }
 
        // Triplet found
        if (found)
            break;
    }
 
    // Triplet not found
    if (!found)
        System.out.print("-1");
}
 
// Driver code
public static void main(String[] args)
{
    int x = 105;
 
    findTriplets(x);
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
from math import sqrt
 
# Function to find the required triplets
def findTriplets(x) :
 
    # To store the factors
    fact = [];
    factors = set();
 
    # Find factors in sqrt(x) time
    for i in range(2, int(sqrt(x))) :
        if (x % i == 0) :
            fact.append(i);
             
            if (x / i != i) :
                fact.append(x // i);
                 
            factors.add(i);
            factors.add(x // i);
 
    found = False;
    k = len(fact);
     
    for i in range(k) :
 
        # Choose a factor
        a = fact[i];
         
        for j in range(k) :
 
            # Choose another factor
            b = fact[j];
 
            # These conditions need to be
            # met for a valid triplet
            if ((a != b) and (x % (a * b) == 0)
                and (x / (a * b) != a)
                and (x / (a * b) != b)
                and (x / (a * b) != 1)) :
 
                # Print the valid triplet
                print(a,b,x // (a * b));
                found = True;
                break;
     
        # Triplet found
        if (found) :
            break;
 
    # Triplet not found
    if (not found) :
        print("-1");
 
# Driver code
if __name__ == "__main__" :
 
    x = 105;
 
    findTriplets(x);
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to find the required triplets
static void findTriplets(int x)
{
    // To store the factors
    List fact = new List();
    HashSet factors = new HashSet();
 
    // Find factors in Math.Sqrt(x) time
    for (int i = 2; i <= Math.Sqrt(x); i++)
    {
        if (x % i == 0)
        {
            fact.Add(i);
            if (x / i != i)
                fact.Add(x / i);
            factors.Add(i);
            factors.Add(x / i);
        }
    }
 
    bool found = false;
    int k = fact.Count;
    for (int i = 0; i < k; i++)
    {
 
        // Choose a factor
        int a = fact[i];
        for (int j = 0; j < k; j++)
        {
 
            // Choose another factor
            int b = fact[j];
 
            // These conditions need to be
            // met for a valid triplet
            if ((a != b) && (x % (a * b) == 0)
                && (x / (a * b) != a)
                && (x / (a * b) != b)
                && (x / (a * b) != 1))
            {
 
                // Print the valid triplet
                Console.Write(a+ " " + b + " "
                    + (x / (a * b)));
                found = true;
                break;
            }
        }
 
        // Triplet found
        if (found)
            break;
    }
 
    // Triplet not found
    if (!found)
        Console.Write("-1");
}
 
// Driver code
public static void Main(String[] args)
{
    int x = 105;
 
    findTriplets(x);
}
}
 
// This code is contributed by 29AjayKumar


输出:
3 5 7

时间复杂度: O(N),N = X

辅助空间: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。