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

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 = 3
Output: 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 = 100
Output: 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++
// 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
// Java program to implement // the above approach import 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 |
Python3
# 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#
// C# program to implement // the above approach using 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 |
Javascript
<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> |
Output:Â
3
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



