计算产品不超过给定数量的三胞胎数量
给定一个正整数N ,任务是找出乘积最多为 N的正整数(X, Y, Z)三元组的数量。
例子:
Input: N = 2
Output: 4
Explanation: Below are the triplets whose product is at most N(= 2):
- (1, 1, 1): Product is 1*1*1 = 1.
- (1, 1, 2): Product is 1*1*2 = 2.
- (1, 2, 1): Product is 1*2*1 = 2.
- (2, 1, 1): Product is 2*1*1 = 2.
Therefore, the total count is 4.
Input: 6
Output: 25
朴素方法:解决给定问题的最简单方法是生成所有可能的三元组,其值位于[0, N]范围内,并计算那些值的乘积最多为 N的三元组。检查所有三元组后,打印获得的总数。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:上述方法也可以通过在[1, N]范围内生成所有可能的对(i, j)并将所有可能对的计数增加N / (i * j)来优化。请按照以下步骤解决问题:
- 初始化一个变量,比如ans ,它存储所有可能的三元组的计数。
- 在[1, N]范围内生成所有可能的对(i, j) ,如果这些对的乘积大于N ,则检查下一对。否则,将所有可能对的计数增加N/(i*j) 。
- 完成上述步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// triplets whose product is at most N
int countTriplets(int N)
{
// Stores the count of triplets
int ans = 0;
// Iterate over the range [0, N]
for (int i = 1; i <= N; i++) {
// Iterate over the range [0, N]
for (int j = 1; j <= N; j++) {
// If the product of
// pairs exceeds N
if (i * j > N)
break;
// Increment the count of
// possible triplets
ans += N / (i * j);
}
}
// Return the total count
return ans;
}
// Driver Code
int main()
{
int N = 10;
cout << countTriplets(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count the number of
// triplets whose product is at most N
static int countTriplets(int N)
{
// Stores the count of triplets
int ans = 0;
// Iterate over the range [0, N]
for(int i = 1; i <= N; i++)
{
// Iterate over the range [0, N]
for(int j = 1; j <= N; j++)
{
// If the product of
// pairs exceeds N
if (i * j > N)
break;
// Increment the count of
// possible triplets
ans += N / (i * j);
}
}
// Return the total count
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
System.out.print(countTriplets(N));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to count the number of
# triplets whose product is at most N
def countTriplets(N):
# Stores the count of triplets
ans = 0
# Iterate over the range [0, N]
for i in range(1, N + 1):
# Iterate over the range [0, N]
for j in range(1, N + 1):
# If the product of
# pairs exceeds N
if (i * j > N):
break
# Increment the count of
# possible triplets
ans += N // (i * j)
# Return the total count
return ans
# Driver Code
if __name__ == "__main__":
N = 10
print(countTriplets(N))
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number of
// triplets whose product is at most N
static int countTriplets(int N)
{
// Stores the count of triplets
int ans = 0;
// Iterate over the range [0, N]
for(int i = 1; i <= N; i++)
{
// Iterate over the range [0, N]
for(int j = 1; j <= N; j++)
{
// If the product of
// pairs exceeds N
if (i * j > N)
break;
// Increment the count of
// possible triplets
ans += N / (i * j);
}
}
// Return the total count
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int N = 10;
Console.Write(countTriplets(N));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
53
时间复杂度: O(N 2 )
辅助空间: O(1)