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 $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
< ? php function generateNumbers ( $ factor , $ n , $ k ) { $ next = array_fill ( 0 , $ k , 0 ) ; for ( $ i = 0 ; $ i < $ n ; ) { $ toincrement = 0 ; for ( $ j = 0 ; $ j < $ k ; $ j ++ ) if ( $ next [ $ j ] < $ next [ $ toincrement ] ) $ toincrement = $ j ; if ( $ output != $ next [ $ toincrement ] ) { $ output = $ next [ $ toincrement ] ; echo $ next [ $ toincrement ] . " " ; $ i ++ ; } $ next [ $ toincrement ] += $ factor [ $ toincrement ] ; } } $ factor = array ( 3 , 5 , 7 ) ; $ n = 10 ; $ k = count ( $ factor ) ; generateNumbers ( $ factor , $ n , $ k ) ; ? >