Count of triplets till N whose product is at most N
Last Updated :
11 Nov, 2023
Given a positive integer N, the task is to find the number of triplets (A, B, C) from the first N Natural Numbers such that A * B * C ≤ N.
Examples:
Input: N = 2
Output: 4
Explanation:
Following are the triplets that satisfy the given criteria:
- ( 1, 1, 1 ) => 1 * 1 * 1 = 1 ≤ 2.
- ( 1, 1, 2 ) => 1 * 1 * 2 = 2 ≤ 2.
- ( 1, 2, 1 ) => 1 * 2 * 1 = 2 ≤ 2.
- ( 2, 1, 1 ) => 2 * 1 * 1 = 2 ≤ 2.
Therefore, the total count of triplets is 4.
Input: N = 10
Output: 53
Naive Approach: The simplest approach to solve the given problem is to generate all possible triplets from the first N natural numbers and count those triplets that satisfy the given criteria. After checking for all the triplets, print the total count obtained.
C++
#include <iostream>
using namespace std;
int countTriplets( int n) {
int count = 0;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= n; j++) {
for ( int k = 1; k <= n; k++) {
if (i * j * k <= n) {
count++;
}
}
}
}
return count;
}
int main() {
int N = 4;
cout << countTriplets(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 4 ;
System.out.println(countTriplets(N));
}
public static int countTriplets( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= n; j++) {
for ( int k = 1 ; k <= n; k++) {
if (i * j * k <= n) {
count++;
}
}
}
}
return count;
}
}
|
Python3
def count_triplets(n):
count = 0
for i in range ( 1 , n + 1 ):
for j in range ( 1 , n + 1 ):
for k in range ( 1 , n + 1 ):
if i * j * k < = n:
count + = 1
return count
def main():
N = 4
print (count_triplets(N))
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG
{
public static void Main( string [] args)
{
int N = 4;
Console.WriteLine(CountTriplets(N));
}
public static int CountTriplets( int n)
{
int count = 0;
for ( int i = 1; i <= n; i++)
{
for ( int j = 1; j <= n; j++)
{
for ( int k = 1; k <= n; k++)
{
if (i * j * k <= n)
{
count++;
}
}
}
}
return count;
}
}
|
Javascript
<script>
function countTriplets(n) {
let count = 0;
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
for (let k = 1; k <= n; k++) {
if (i * j * k <= n) {
count++;
}
}
}
}
return count;
}
const N = 4;
console.log(countTriplets(N));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by the observation that if A and B are fixed, then it is possible to calculate all the possible choices for C by doing N/(A*B) because N/(A*B) will give the maximum value, say X, which on multiplication with (A*B) results in a value less than or equal to N. So, all possible choices of C will be from 1 to X. Now, A and B can be fixed by trying A for every possible number till N, and B for every possible number till (N/A). Follow the steps below to solve the given problem:
- Initialize variable, say cnt as 0 that stores the count of triplets possible.
- Iterate a loop over the range [1, N] using the variable i and nested iterate over the range [1, N] using the variable j and increment the value of cnt by the value of cnt/(i*j).
- After completing the above steps, print the value of cnt as the result.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int N)
{
int cnt = 0;
for ( int A = 1; A <= N; ++A) {
for ( int B = 1; B <= N / A; ++B) {
cnt += N / (A * B);
}
}
return cnt;
}
int main()
{
int N = 2;
cout << countTriplets(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countTriplets( int N)
{
int cnt = 0 ;
for ( int A = 1 ; A <= N; ++A) {
for ( int B = 1 ; B <= N / A; ++B) {
cnt += N / (A * B);
}
}
return cnt;
}
public static void main(String[] args)
{
int N = 2 ;
System.out.println(countTriplets(N));
}
}
|
Python3
def countTriplets(N) :
cnt = 0 ;
for A in range ( 1 , N + 1 ) :
for B in range ( 1 , N / / A + 1 ) :
cnt + = N / / (A * B);
return cnt;
if __name__ = = "__main__" :
N = 2 ;
print (countTriplets(N));
|
C#
using System;
public class GFG {
static int countTriplets( int N)
{
int cnt = 0;
for ( int A = 1; A <= N; ++A) {
for ( int B = 1; B <= N / A; ++B) {
cnt += N / (A * B);
}
}
return cnt;
}
public static void Main( string [] args)
{
int N = 2;
Console.WriteLine(countTriplets(N));
}
}
|
Javascript
<script>
function countTriplets(N) {
let cnt = 0;
for (let A = 1; A <= N; ++A) {
for (let B = 1; B <= N / A; ++B) {
cnt += N / (A * B);
}
}
return cnt;
}
let N = 2;
document.write(countTriplets(N));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...