Count of sorted triplets (a, b, c) whose product is at most N - GeeksforGeeks

24 Dec, 2021
Given an integer N, the task is to find the count of triplets (a, b, c) such that a <= b <= c and a * b * c <= N.
Examples:
Input: N = 5Output: 6Explanation: The triplets that follow the required conditions are (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5) and (1, 2, 2). Hence the count of value triplets is 6. 
Input: N = 20Output: 40
Approach: The given problem can be solved based on the following observations:
Since a <= b <=c, it can be observed that the value of a must lie in the range [1, N1/3].
Similarly, for a given a the value of b must lie in the range [a, (N/a)1/2].
Similarly, when the value of a and b is fixed, the value of c must lie in the range [b, N/(a*b)]. Hence the number of possible values of c is the count of integers in the range [b, N/(a*b)]. Therefore, for each valid a and b, the number of possible values of c are N/(a*b) – b + 1.
Therefore, using the above observations below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <iostream>using namespace std; // Function to find the count of valid// triplets (a, b, c) such that the value// of a * b * c <= N and a <= b <= clong long validTriplets(int N){    // Stores count of triplets    long long ans = 0;     // Loop to iterate in the    // range [1, N^(1/3)]    for (long long a = 1; a * a * a <= N; a++) {         // Loop to iterate in the        // range [a, (N/a)^(1/2)]        for (long long b = a; a * b * b <= N; b++) {             // Add the count of valid            // values of c for a fixed            // value of a and b            ans += N / a / b - b + 1;        }    }     // Return Answer    return ans;} // Driver Codeint main(){    int N = 5;    cout << validTriplets(N);     return 0;}
// Java implementation of the above approachimport java.util.*; class GFG{ // Function to find the count of valid// triplets (a, b, c) such that the value// of a * b * c <= N and a <= b <= cstatic long validTriplets(int N){       // Stores count of triplets    long ans = 0;     // Loop to iterate in the    // range [1, N^(1/3)]    for (long a = 1; a * a * a <= N; a++) {         // Loop to iterate in the        // range [a, (N/a)^(1/2)]        for (long b = a; a * b * b <= N; b++) {             // Add the count of valid            // values of c for a fixed            // value of a and b            ans += N / a / b - b + 1;        }    }     // Return Answer    return ans;} // Driver Codepublic static void main(String[] args){    int N = 5;    System.out.print(validTriplets(N)); }} // This code is contributed by 29AjayKumar
# Python implementation of the above approach # Function to find the count of valid# triplets (a, b, c) such that the value# of a * b * c <= N and a <= b <= cdef validTriplets(N):         # Stores count of triplets    ans = 0;     # Loop to iterate in the    # range [1, N^(1/3)]    for a in range(1, int(N ** (1 / 3)) + 1):             # Loop to iterate in the        # range [a, (N/a)^(1/2)]        b = a;        while(a * b * b <= N):            # Add the count of valid            # values of c for a fixed            # value of a and b            ans += N / a / b - b + 1;            b += 1         # Return Answer    return int(ans); # Driver CodeN = 5; print(validTriplets(N)); # This code is contributed by gfgking
// C# implementation of the above approachusing System;class GFG {    // Function to find the count of valid    // triplets (a, b, c) such that the value    // of a * b * c <= N and a <= b <= c    static long validTriplets(int N)    {        // Stores count of triplets        long ans = 0;         // Loop to iterate in the        // range [1, N^(1/3)]        for (long a = 1; a * a * a <= N; a++) {             // Loop to iterate in the            // range [a, (N/a)^(1/2)]            for (long b = a; a * b * b <= N; b++) {                 // Add the count of valid                // values of c for a fixed                // value of a and b                ans += N / a / b - b + 1;            }        }         // Return Answer        return ans;    }     // Driver Code    public static void Main()    {        int N = 5;        Console.WriteLine(validTriplets(N));    }} // This code is contributed by ukasp.
<script> // JavaScript implementation of the above approach // Function to find the count of valid// triplets (a, b, c) such that the value// of a * b * c <= N and a <= b <= cfunction validTriplets(N){         // Stores count of triplets    let ans = 0;     // Loop to iterate in the    // range [1, N^(1/3)]    for(let a = 1; a * a * a <= N; a++)    {                 // Loop to iterate in the        // range [a, (N/a)^(1/2)]        for(let b = a; a * b * b <= N; b++)        {                         // Add the count of valid            // values of c for a fixed            // value of a and b            ans += N / a / b - b + 1;        }    }         // Return Answer    return Math.floor(ans);} // Driver Codelet N = 5; document.write(validTriplets(N)); // This code is contributed by Potta Lokesh </script>
6
Time Complexity: O(N2/3)Auxiliary Space: O(1)
 
lokeshpotta20
gfgking
ukasp
29AjayKumar
khushboogoyal499
Combinatorial
Greedy
Mathematical
Searching
Searching
Greedy
Mathematical
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find the Number of Permutations that satisfy the given condition in an array
Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Number of Simple Graph with N Vertices and M Edges
Largest substring with same Characters
Dijkstra's shortest path algorithm | Greedy Algo-7
Program for array rotation
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3