Count of triplets having sum of product of any two numbers with the third number equal to N
Last Updated :
18 Jan, 2023
Given an positive integer N, the task is to find the number of triplets (X, Y, Z) such that the sum of product of any two numbers with the third number is N.
Examples:
Input: N = 2
Output: 1
Explanation:
The only triplets satisfying the given criteria is (1, 1, 1). Therefore, the count is 1.
Input: N = 3
Output: 3
Approach: This given problem can be solved by rearranging the equation as:
Consider a triplet as (X, Y, Z) then
=>
=>
From the above equation the idea is to iterate over all possible value of Z over the range [1, N] and add the count of all possible of X and Y by calculating the prime factor of (N – Z) satisfying the above equation. Follow the below steps to solve the problem:
- Initialize a variable, countTriplets to store the resultant count of triplets satisfying the given criteria.
- Find the smallest prime factor for all the elements over the range [1, 105] using the Sieve Of Eratosthenes.
- Iterate over the range [1, N] using the variable, say K and perform the following steps:
- Find the number of pairs of whose product is (N – K) using the approach discussed in this article and add the count obtained to the variable countTriplets.
- After completing the above steps, print the value of countTriplets as the resultant count of triplets.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
vector< int > s(11,0);
void sieveOfEratosthenes( int N){
bool prime[N+1];
memset (prime, false , sizeof ( false ));
for ( int i=2;i< N + 1; i+=2)
s[i] = 2;
for ( int i =3;i<N + 1;i+=2){
if (prime[i] == false ){
s[i] = i;
for ( int j= i;j< N / i + 1; j+=2){
if (prime[i * j] == false ){
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
int generatePrimeFactors( int N){
int curr = s[N];
map< int , int > cnt;
cnt[s[N]] = 1;
while (N > 1){
N /= s[N];
if (N and s[N])
if (cnt.find(s[N]) == cnt.end())
cnt[s[N]] = 1;
else
cnt[s[N]] += 1;
}
if (cnt.find(0) != cnt.end())
cnt.erase(0);
int totfactor = 1;
for ( auto i: cnt)
totfactor *= i.second + 1;
return totfactor;
}
int countTriplets( int N){
int CountTriplet = 0;
for ( int z=1;z<N + 1;z++){
int p = generatePrimeFactors(N-z);
if (p > 1)
CountTriplet += p;
}
return CountTriplet + 1;
}
int main(){
int N = 10;
sieveOfEratosthenes(N);
cout<<countTriplets(N);
}
|
Java
import java.util.*;
class GFG{
static int [] s = new int [ 11 ];
static void sieveOfEratosthenes( int N){
boolean []prime = new boolean [N+ 1 ];
for ( int i = 2 ; i < N + 1 ; i += 2 )
s[i] = 2 ;
for ( int i = 3 ; i < N + 1 ; i += 2 ){
if (prime[i] == false ){
s[i] = i;
for ( int j= i; j < N / i + 1 ; j += 2 ){
if (prime[i * j] == false ){
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
static int generatePrimeFactors( int N){
int curr = s[N];
HashMap<Integer,Integer> cnt = new HashMap<>();
cnt.put(s[N], 1 );
while (N > 1 ){
N /= s[N];
if (N != 0 && s[N] != 0 )
if (!cnt.containsKey(s[N]))
cnt.put(s[N], 1 );
else
cnt.put(s[N], cnt.get(s[N]) + 1 );
}
if (cnt.containsKey( 0 ))
cnt.remove( 0 );
int totfactor = 1 ;
for (Map.Entry<Integer,Integer> i : cnt.entrySet())
totfactor *= i.getValue() + 1 ;
return totfactor;
}
static int countTriplets( int N){
int CountTriplet = 0 ;
for ( int z= 1 ;z<N + 1 ;z++){
int p = generatePrimeFactors(N-z);
if (p > 1 )
CountTriplet += p;
}
return CountTriplet + 1 ;
}
public static void main(String[] args){
int N = 10 ;
sieveOfEratosthenes(N);
System.out.print(countTriplets(N));
}
}
|
Python3
def sieveOfEratosthenes(N, s):
prime = [ False ] * (N + 1 )
for i in range ( 2 , N + 1 , 2 ):
s[i] = 2
for i in range ( 3 , N + 1 , 2 ):
if (prime[i] = = False ):
s[i] = i
for j in range (i, int (N / i) + 1 , 2 ):
if (prime[i * j] = = False ):
prime[i * j] = True
s[i * j] = i
def generatePrimeFactors(N):
curr = s[N]
cnt = {s[N]: 1 }
while (N > 1 ):
N / / = s[N]
if N and s[N]:
if cnt.get(s[N], 0 ) = = 0 :
cnt[s[N]] = 1
else :
cnt[s[N]] + = 1
if 0 in cnt:
cnt.pop( 0 )
totfactor = 1
for i in cnt.values():
totfactor * = i + 1
return totfactor
def countTriplets(N):
CountTriplet = 0
for z in range ( 1 , N + 1 ):
p = generatePrimeFactors(N - z)
if p > 1 :
CountTriplet + = p
return CountTriplet + 1
N = 10
s = [ 0 ] * (N + 1 )
sieveOfEratosthenes(N, s)
print (countTriplets(N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int [] s = new int [11];
static void sieveOfEratosthenes( int N)
{
bool [] prime = new bool [N + 1];
for ( int i = 2; i < N + 1; i += 2)
s[i] = 2;
for ( int i = 3; i < N + 1; i += 2)
{
if (prime[i] == false )
{
s[i] = i;
for ( int j = i; j < N / i + 1; j += 2)
{
if (prime[i * j] == false )
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
static int generatePrimeFactors( int N)
{
Dictionary< int , int > cnt = new Dictionary< int , int >();
cnt.Add(s[N], 1);
while (N > 1)
{
N /= s[N];
if (N != 0 && s[N] != 0)
if (!cnt.ContainsKey(s[N]))
cnt.Add(s[N], 1);
else
cnt[s[N]]++;
}
if (cnt.ContainsKey(0))
cnt.Remove(0);
int totfactor = 1;
foreach ( var entry in cnt)
totfactor *= entry.Value + 1;
return totfactor;
}
static int countTriplets( int N)
{
int countTriplet = 0;
for ( int z = 1; z < N + 1; z++)
{
int p = generatePrimeFactors(N - z);
if (p > 1)
countTriplet += p;
}
return countTriplet + 1;
}
static void Main( string [] args)
{
int N = 10;
sieveOfEratosthenes(N);
Console.WriteLine(countTriplets(N));
}
}
|
Javascript
<script>
let s = new Array(11).fill(0)
function sieveOfEratosthenes(N){
let prime = new Array(N+1).fill( false );
for (let i = 2; i < N + 1; i += 2)
s[i] = 2;
for (let i = 3; i < N + 1; i += 2){
if (prime[i] == false ){
s[i] = i;
for (let j= i; j < Math.floor(N / i + 1); j += 2){
if (prime[i * j] == false ){
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
function generatePrimeFactors(N)
{
let curr = s[N];
let cnt = new Map();
cnt.set(s[N],1);
while (N > 1){
N = Math.floor(N / s[N]);
if (N != 0 && s[N] != 0)
if (!cnt.has(s[N]))
cnt.set(s[N], 1);
else
cnt.set(s[N], cnt.get(s[N]) + 1);
}
if (cnt.has(0))
cnt. delete (0);
let totfactor = 1;
for (i of cnt.values())
totfactor *= i + 1;
return totfactor;
}
function countTriplets(N){
let CountTriplet = 0;
for (let z=1;z<N + 1;z++){
let p = generatePrimeFactors(N-z);
if (p > 1)
CountTriplet += p;
}
return CountTriplet + 1;
}
let N = 10;
sieveOfEratosthenes(N);
document.write((countTriplets(N)));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...