Generation of n numbers with given set of factors | C ++ program to generate n numbers with given factors ; Generate n numbers with factors in factor [ ] ; array of k to store next multiples of given factors ; Prints n numbers int 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateNumbers ( int factor [ ] , int n , int k ) { int next [ k ] = { 0 } ; for ( int i = 0 ; i < n ; ) { int toincrement = 0 ; for ( int j = 0 ; j < k ; j ++ ) if ( next [ j ] < next [ toincrement ] ) toincrement = j ; if ( output != next [ toincrement ] ) { output = next [ toincrement ] ; printf ( " % d ▁ " , next [ toincrement ] ) ; i ++ ; } next [ toincrement ] += factor [ toincrement ] ; } } int main ( ) { int factor [ ] = { 3 , 5 , 7 } ; int n = 10 ; int k = sizeof ( factor ) / sizeof ( factor [ 0 ] ) ; generateNumbers ( factor , n , k ) ; return 0 ; }