Count triplets such that product of two numbers added with third number is N
Last Updated :
11 Oct, 2021
Given a positive integer N, the task is to find the number of triplets (A, B, C) where A, B, C are positive integers such that the product of two numbers added with the third number is N i.e., A * B + C = N.
Examples:
Input: N = 3
Output: 3
Explanation:
Following are the possible triplets satisfying the given criteria:
- (1, 1, 2): The value of 1*1 + 2 = 3.
- (1, 2, 1): The value of 1*2 + 1 = 3.
- (2, 1, 1): The value of 2*1 + 1 = 3.
Therefore, the total count of such triplets is 3.
Input: N = 5
Output: 8
Approach: The given problem can be solved by rearranging the equation A * B + C = N as A * B = N – C. Now, the only possible values A and B can have to satisfy the above equation is the divisors of N – C. For Example, if the value of N – C = 18, having 6 divisors that are 1, 2, 3, 6, 9, 18. So, values of A, B satisfying the above equation are: (1, 18), (2, 9), (3, 6), (6, 3), (9, 2), (18, 1). So, for the value of N – C = 18, possible values of A, B are 6, i.e., the number of divisors of N – C(= 18). Follow the steps below to solve the given problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDivisors( int n)
{
int divisors = 0;
int i;
for (i = 1; i * i < n; i++) {
if (n % i == 0) {
divisors++;
}
}
if (i - (n / i) == 1) {
i--;
}
for (; i >= 1; i--) {
if (n % i == 0) {
divisors++;
}
}
return divisors;
}
int possibleTriplets( int N)
{
int count = 0;
for ( int i = 1; i < N; i++) {
count += countDivisors(N - i);
}
return count;
}
int main()
{
int N = 10;
cout << possibleTriplets(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countDivisors( int n)
{
int divisors = 0 ;
int i;
for (i = 1 ; i * i < n; i++) {
if (n % i == 0 ) {
divisors++;
}
}
if (i - (n / i) == 1 ) {
i--;
}
for (; i >= 1 ; i--) {
if (n % i == 0 ) {
divisors++;
}
}
return divisors;
}
static int possibleTriplets( int N)
{
int count = 0 ;
for ( int i = 1 ; i < N; i++) {
count += countDivisors(N - i);
}
return count;
}
public static void main (String[] args) {
int N = 10 ;
System.out.println(possibleTriplets(N));
}
}
|
Python3
import math
def countDivisors(n):
divisors = 0
for i in range ( 1 , math.ceil(math.sqrt(n)) + 1 ):
if n % i = = 0 :
divisors = divisors + 1
if (i - (n / i) = = 1 ):
i = i - 1
for i in range (math.ceil(math.sqrt(n)) + 1 , 1 , - 1 ):
if (n % i = = 0 ):
divisors = divisors + 1
return divisors
def possibleTriplets(N):
count = 0
for i in range ( 1 , N):
count = count + countDivisors(N - i)
return count
if __name__ = = "__main__" :
N = 10
print (possibleTriplets(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countDivisors( int n)
{
int divisors = 0;
int i;
for (i = 1; i * i < n; i++) {
if (n % i == 0) {
divisors++;
}
}
if (i - (n / i) == 1) {
i--;
}
for (; i >= 1; i--) {
if (n % i == 0) {
divisors++;
}
}
return divisors;
}
static int possibleTriplets( int N)
{
int count = 0;
for ( int i = 1; i < N; i++) {
count += countDivisors(N - i);
}
return count;
}
public static void Main()
{
int N = 10;
Console.Write(possibleTriplets(N));
}
}
|
Javascript
<script>
function countDivisors(n)
{
var divisors = 0;
var i;
for (i = 1; i * i < n; i++) {
if (n % i == 0) {
divisors++;
}
}
if (i - (n / i) == 1) {
i--;
}
for (; i >= 1; i--) {
if (n % i == 0) {
divisors++;
}
}
return divisors;
}
function possibleTriplets(N)
{
var count = 0;
for ( var i = 1; i < N; i++) {
count += countDivisors(N - i);
}
return count;
}
var N = 10;
document.write(possibleTriplets(N));
</script>
|
Time Complexity: O(N*sqrt(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...