Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Count of triplets till N whose product is at most N

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 = 3
Output: 4
Explanation:
Following are the triplets that satisfy the given criteria:

  1. ( 1, 1, 1 ) => 1 * 1 * 1 = 1 ≤  2.
  2. ( 1, 1, 2 ) => 1 * 1 * 2 = 2 ≤  2.
  3. ( 1, 2, 1 ) => 1 * 2 * 1 = 2 ≤ 2.
  4. ( 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.

Java




/*package whatever //do not write package name here */
 
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;
    }
}
//This code is contributed by aeroabrar_31

Output

4

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
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




<script>
        // JavaScript Program to implement
        // the above approach
 
 
        // Function to find number of triplets
        // (A, B, C) having A * B * C <= N
        function countTriplets(N) {
            // Stores the count of triplets
            let cnt = 0;
 
            // Iterate a loop fixing the value
            // of A
            for (let A = 1; A <= N; ++A) {
 
                // Iterate a loop fixing the
                // value of A
                for (let 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
 
        let N = 2;
        document.write(countTriplets(N));
 
 
// This code is contributed by Potta Lokesh
    </script>

 
 

Output: 

4

 

 

Time Complexity: O(N * log N)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Last Updated : 28 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials