Count of triplets having sum of product of any two numbers with the third number equal to N | Javascript program for the above approach ; Function to find the SPF [ i ] using the Sieve Of Erastothenes ; Stores whether i is prime or not ; Initializing smallest factor as 2 for all even numbers ; Iterate for all odd numbers < N ; SPF of i for a prime is the number itself ; Iterate for all the multiples of the current prime number ; The value i is smallest prime factor for i * j ; Function to generate prime factors and its power ; Current prime factor of N ; Stores the powers of the current prime factor ; Find all the prime factors and their powers ; Return the total count of factors ; Function to count the number of triplets satisfying the given criteria ; Stores the count of resultant triplets ; Add the count all factors of N - z to the variable CountTriplet ; Return total count of triplets ; Driver Code ; Find the SPF [ i ] ; Function Call
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 ) ) ) ;