Find the Nth term divisible by a or b or c | Function to return gcd of a and b ; Function to return the lcm of a and b ; Function to return the count of numbers from 1 to num which are divisible by a , b or c ; Calculate number of terms divisible by a and by b and by c then , remove the terms which is are divisible by both a and b , both b and c , both c and a and then add which are divisible by a and b and c ; Function to find the nth term divisible by a , b or c by using binary search ; Set low to 1 and high to max ( a , b , c ) * n ; If the current term is less than n then we need to increase low to mid + 1 ; If current term is greater than equal to n then high = mid ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function lcm ( a , b ) { return parseInt ( ( a * b ) / gcd ( a , b ) ) ; } function divTermCount ( a , b , c , num ) { return ( parseInt ( num / a ) + parseInt ( num / b ) + parseInt ( num / c ) - parseInt ( num / lcm ( a , b ) ) - parseInt ( num / lcm ( b , c ) ) - parseInt ( num / lcm ( a , c ) ) + parseInt ( num / lcm ( a , lcm ( b , c ) ) ) ) ; } function findNthTerm ( a , b , c , n ) { let low = 1 , high = Number . MAX_VALUE , mid ; while ( low < high ) { mid = low + parseInt ( ( high - low ) / 2 ) ; if ( divTermCount ( a , b , c , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } let a = 2 , b = 3 , c = 5 , n = 10 ; document . write ( findNthTerm ( a , b , c , n ) ) ;