Count number of triplets (a, b, c) from first N natural numbers such that a * b + c = N - GeeksforGeeks

03 May, 2021
Given an integer N, the task is to count the triplets (a, b, c) from the first N natural numbers such that a * b + c = N.
Examples:
Input: N = 3Output: 3 Explanation: Triplets of the form a * b + c = N are { (1, 1, 2), (1, 2, 1), (2, 1, 1) } Therefore, the required output is 3.
Input: N = 100Output: 473
Approach: The problem can be solved based on the following observation:
For every possible pairs (a, b), If a * b < N, then only c exists. Therefore, count the pairs (a, b) whose product is less than N.
Follow the steps below to solve the problem:
Initialize a variable, say cntTriplets, to store the count of triplets of first N natural numbers that satisfy the given condition.
Iterate over the range [1, N – 1] using variable i and check if N % i == 0 or not. If found to be true, then update cntTriplets += (N / i) – 1.
Otherwise, update cntTriplets += (N / i).
Finally, print the value of cntTriplets.
Below is the implementation of the above approach.
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to find the count of// triplets (a, b, c) with a * b + c = Nint findCntTriplet(int N){    // Stores count of triplets of 1st    // N natural numbers which are of    // the form a * b + c = N    int cntTriplet = 0;     // Iterate over the range [1, N]    for (int i = 1; i < N; i++) {         // If N is divisible by i        if (N % i != 0) {             // Update cntTriplet            cntTriplet += N / i;        }        else {             // Update cntTriplet            cntTriplet += (N / i) - 1;        }    }    return cntTriplet;} // Driver Codeint main(){    int N = 3;    cout << findCntTriplet(N);    return 0;}
// Java program to implement// the above approachimport java.util.*;class GFG{   // Function to find the count of  // triplets (a, b, c) with a * b + c = N  static int findCntTriplet(int N)  {     // Stores count of triplets of 1st    // N natural numbers which are of    // the form a * b + c = N    int cntTriplet = 0;     // Iterate over the range [1, N]    for (int i = 1; i < N; i++)    {       // If N is divisible by i      if (N % i != 0)      {         // Update cntTriplet        cntTriplet += N / i;      }      else      {         // Update cntTriplet        cntTriplet += (N / i) - 1;      }    }    return cntTriplet;  }   // Driver code  public static void main(String[] args)  {    int N = 3;    System.out.println(findCntTriplet(N));  }} // This code is contributed by susmitakundugoaldanga
# Python program to implement# the above approach # Function to find the count of# triplets (a, b, c) with a * b + c = Ndef findCntTriplet(N):       # Stores count of triplets of 1st    # N natural numbers which are of    # the form a * b + c = N    cntTriplet = 0;     # Iterate over the range [1, N]    for i in range(1, N):         # If N is divisible by i        if (N % i != 0):             # Update cntTriplet            cntTriplet += N // i;        else:             # Update cntTriplet            cntTriplet += (N // i) - 1;     return cntTriplet; # Driver codeif __name__ == '__main__':    N = 3;    print(findCntTriplet(N)); # This code is contributed by 29AjayKumar
// C# program to implement// the above approachusing System;class GFG{   // Function to find the count of  // triplets (a, b, c) with a * b + c = N  static int findCntTriplet(int N)  {     // Stores count of triplets of 1st    // N natural numbers which are of    // the form a * b + c = N    int cntTriplet = 0;     // Iterate over the range [1, N]    for (int i = 1; i < N; i++)    {       // If N is divisible by i      if (N % i != 0)      {         // Update cntTriplet        cntTriplet += N / i;      }      else      {         // Update cntTriplet        cntTriplet += (N / i) - 1;      }    }    return cntTriplet;  }   // Driver code  public static void Main(String[] args)  {    int N = 3;    Console.WriteLine(findCntTriplet(N));  }} // This code is contributed by 29AjayKumar
<script> // javascript program for the above approach    // Function to find the count of  // triplets (a, b, c) with a * b + c = N  function findCntTriplet(N)  {     // Stores count of triplets of 1st    // N natural numbers which are of    // the form a * b + c = N    let cntTriplet = 0;     // Iterate over the range [1, N]    for (let i = 1; i < N; i++)    {       // If N is divisible by i      if (N % i != 0)      {         // Update cntTriplet        cntTriplet += Math.floor(N / i);      }      else      {         // Update cntTriplet        cntTriplet += Math.floor(N / i) - 1;      }    }    return cntTriplet;  } // Driver Code         let N = 3;    document.write(findCntTriplet(N));     </script>
3
 
Time Complexity: O(N)Auxiliary Space: O(1)
susmitakundugoaldanga
29AjayKumar
souravghosh0416
divisibility
Greedy
Mathematical
Searching
Searching
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Huffman Coding | Greedy Algo-3
Activity Selection Problem | Greedy Algo-1
Coin Change | DP-7
Fractional Knapsack Problem
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Merge two sorted arrays