Generation of n numbers with given set of factors | Generate n numbers with factors in factor [ ] ; Array of k to store next multiples of given factors ; Prints n numbers let output = 0 ; Next number to print as output ; Find the next smallest multiple ; Printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; Incrementing the current value by the respective factor ; Driver code
function generateNumbers ( factor , n , k ) { let next = new Array ( k ) ; next . fill ( 0 ) ; for ( let i = 0 ; i < n ; ) { let toincrement = 0 ; for ( let j = 0 ; j < k ; j ++ ) if ( next [ j ] < next [ toincrement ] ) toincrement = j ; if ( output != next [ toincrement ] ) { output = next [ toincrement ] ; document . write ( next [ toincrement ] + " " ) ; i ++ ; } next [ toincrement ] += factor [ toincrement ] ; } } let factor = [ 3 , 5 , 7 ] ; let n = 10 ; let k = factor . length ; generateNumbers ( factor , n , k ) ;