📌  相关文章
📜  直到 N 的三元组计数,其乘积最多为 N

📅  最后修改于: 2022-05-13 01:56:10.001000             🧑  作者: Mango

直到 N 的三元组计数,其乘积最多为 N

给定一个正整数N ,任务是从前 N 个自然数中找出三元组(A, B, C)的数量,使得A * B * C ≤ N

例子:

朴素方法:解决给定问题的最简单方法是从前N个自然数中生成所有可能的三元组,并计算满足给定标准的三元组。检查所有三元组后,打印获得的总数。

时间复杂度: O(N 3 )
辅助空间: O(1)

有效方法:上述方法可以通过以下观察得到优化:如果AB是固定的,则可以通过N/(A*B)计算C的所有可能选择,因为N/(A*B)将给出最大值,比如X ,它与(A*B)相乘会得到一个小于或等于N的值。因此, C的所有可能选择都是从1X 。现在,可以通过对每个可能的数字尝试A直到N来修复AB ,对每个可能的数字尝试B直到(N/A) 。请按照以下步骤解决给定的问题:

  • 初始化变量,例如将cnt设为0 ,它存储可能的三元组数。
  • 使用变量i在范围[1, N]上迭代一个循环,并使用变量j在范围[1, N]上进行嵌套迭代,并将cnt的值增加cnt/(i*j)的值。
  • 完成上述步骤后,打印cnt的值作为结果。

下面是该方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find number of triplets
// (A, B, C) having A * B * C <= N
int countTriplets(int N)
{
    // Stores the count of triplets
    int cnt = 0;
 
    // Iterate a loop fixing the value
    // of A
    for (int A = 1; A <= N; ++A) {
 
        // Iterate a loop fixing the
        // value of A
        for (int B = 1; B <= N / A; ++B) {
 
            // Find the total count of
            // triplets and add it to cnt
            cnt += N / (A * B);
        }
    }
 
    // Return the total triplets formed
    return cnt;
}
 
// Driver Code
int main()
{
    int N = 2;
    cout << countTriplets(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
    // Function to find number of triplets
    // (A, B, C) having A * B * C <= N
    static int countTriplets(int N)
    {
       
        // Stores the count of triplets
        int cnt = 0;
 
        // Iterate a loop fixing the value
        // of A
        for (int A = 1; A <= N; ++A) {
 
            // Iterate a loop fixing the
            // value of A
            for (int B = 1; B <= N / A; ++B) {
 
                // Find the total count of
                // triplets and add it to cnt
                cnt += N / (A * B);
            }
        }
 
        // Return the total triplets formed
        return cnt;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 2;
 
        System.out.println(countTriplets(N));
    }
}
 
// This code is contributed by dwivediyash


Python3
# Python3 program for the above approach
 
# Function to find number of triplets
# (A, B, C) having A * B * C <= N
def countTriplets(N) :
     
    # Stores the count of triplets
    cnt = 0;
 
    # Iterate a loop fixing the value
    # of A
    for A in range( 1, N + 1) :
 
        # Iterate a loop fixing the
        # value of A
        for B in range(1, N // A + 1) :
 
            # Find the total count of
            # triplets and add it to cnt
            cnt += N // (A * B);
 
    # Return the total triplets formed
    return cnt;
 
# Driver Code
if __name__ == "__main__" :
 
    N = 2;
    print(countTriplets(N));
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
 
public class GFG {
 
    // Function to find number of triplets
    // (A, B, C) having A * B * C <= N
    static int countTriplets(int N)
    {
       
        // Stores the count of triplets
        int cnt = 0;
 
        // Iterate a loop fixing the value
        // of A
        for (int A = 1; A <= N; ++A) {
 
            // Iterate a loop fixing the
            // value of A
            for (int B = 1; B <= N / A; ++B) {
 
                // Find the total count of
                // triplets and add it to cnt
                cnt += N / (A * B);
            }
        }
 
        // Return the total triplets formed
        return cnt;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int N = 2;
 
        Console.WriteLine(countTriplets(N));
    }
}
 
// This code is contributed by AnkThon


Javascript



输出:
4

时间复杂度: O(N * log N)
辅助空间: O(1)