Count of distinct integers belonging to first N terms of at least one of given GPs | Function to find the count of distinct integers that belong to the first N terms of at least one of them is GP ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code
function UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) { let S = new Set ( ) ; let p1 = a1 ; for ( let i = 0 ; i < N ; i ++ ) { S . add ( p1 ) ; p1 = ( p1 * r1 ) ; } let p2 = a2 ; for ( let i = 0 ; i < N ; i ++ ) { S . add ( p2 ) ; p2 = ( p2 * r2 ) ; } return S . size ; } let N = 5 ; let a1 = 3 , r1 = 2 , a2 = 2 , r2 = 3 ; document . write ( UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ) ;