Generation of n numbers with given set of factors
Given an array of k numbers factor[], the task is to print first n numbers (in ascending order) whose factors are from the given array.
Examples:
Input : factor[] = {2, 3, 4, 7}
n = 8
Output : 2 3 4 6 7 8 9 10
Input : factor[] = {3, 5, 7}
n = 10
Output : 3 5 6 7 9 10 12 14 15 18
This problem is mainly an extension of Ugly Number Problem. We create an auxiliary array next[] of size same as factor[] to Keep track of next multiples of factors. In Each iteration, we output the minimum element from next, then increment it by the respective factor. If the minimum value is equal to the previous output value, increment it (to avoid duplicates). Else we print the minimum value.
C/C++
// C++ program to generate n numbers with // given factors #include<bits/stdc++.h> using namespace std; // Generate n numbers with factors in factor[] void generateNumbers(int factor[], int n, int k) { // array of k to store next multiples of // given factors int next[k] = {0}; // Prints n numbers int output = 0; // Next number to print as output for (int i=0; i<n; ) { // Find the next smallest multiple int toincrement = 0; for (int j=0; j<k; j++) if (next[j] < next[toincrement]) toincrement = j; // Printing minimum in each iteration // print the value if output is not equal to // current value(to avoid the duplicates) if (output != next[toincrement]) { output = next[toincrement]; printf("%d ", next[toincrement]); i++; } // incrementing the current value by the // respective factor next[toincrement] += factor[toincrement]; } } // Driver code int main() { int factor[] = {3, 5, 7}; int n = 10; int k = sizeof(factor)/sizeof(factor[0]); generateNumbers(factor, n, k); return 0; } |
Java
// Java program to generate // n numbers with given factors import java.io.*; class GFG { // Generate n numbers with // factors in factor[] static void generateNumbers(int factor[], int n, int k) { // array of k to store // next multiples of // given factors int next[] = new int[k]; // Prints n numbers int output = 0; // Next number to // print as output for (int i = 0; i < n;) { // Find the next // smallest multiple int toincrement = 0; for (int j = 0; j < k; j++) if (next[j] < next[toincrement]) toincrement = j; // Printing minimum in // each iteration print // the value if output // is not equal to current // value(to avoid the // duplicates) if (output != next[toincrement]) { output = next[toincrement]; System.out.print( next[toincrement] + " "); i++; } // incrementing the // current value by the // respective factor next[toincrement] += factor[toincrement]; } } // Driver code public static void main (String[] args) { int factor[] = {3, 5, 7}; int n = 10; int k = factor.length; generateNumbers(factor, n, k); } } // This code is contributed // by ajit |
Python3
# Python3 program to generate n # numbers with given factors # Generate n numbers with # factors in factor[] def generateNumbers(factor, n, k): # array of k to store next # multiples of given factors next = [0] * k; # Prints n numbers output = 0; # Next number to # print as output i = 0; while(i < n): # Find the next smallest # multiple toincrement = 0; for j in range(k): if(next[j] < next[toincrement]): toincrement = j; # Printing minimum in each # iteration print the value # if output is not equal to # current value(to avoid the # duplicates) if(output != next[toincrement]): output = next[toincrement]; print(next[toincrement], end = " "); i += 1; # incrementing the current # value by the respective factor next[toincrement] += factor[toincrement]; # Driver code factor = [3, 5, 7]; n = 10; k = len(factor); generateNumbers(factor, n, k); # This code is contributed by mits |
C#
// C# program to generate // n numbers with given factors using System; class GFG { // Generate n numbers with // factors in factor[] static void generateNumbers(int []factor, int n, int k) { // array of k to store // next multiples of // given factors int []next = new int[k]; // Prints n numbers int output = 0; // Next number to // print as output for (int i = 0; i < n;) { // Find the next // smallest multiple int toincrement = 0; for (int j = 0; j < k; j++) if (next[j] < next[toincrement]) toincrement = j; // Printing minimum in // each iteration print // the value if output // is not equal to current // value(to avoid the // duplicates) if (output != next[toincrement]) { output = next[toincrement]; Console.Write( next[toincrement] + " "); i++; } // incrementing the // current value by the // respective factor next[toincrement] += factor[toincrement]; } } // Driver code static public void Main () { int []factor = {3, 5, 7}; int n = 10; int k = factor.Length; generateNumbers(factor, n, k); } } // This code is contributed // by akt_mit |
PHP
<?php // PHP program to generate n numbers // with given factors // Generate n numbers with factors // in factor[] function generateNumbers($factor, $n, $k) { // array of k to store next // multiples of given factors $next = array_fill(0, $k, 0); // Prints n numbers $output = 0; // Next number to print // as output for ($i = 0; $i < $n; ) { // Find the next smallest multiple $toincrement = 0; for ($j = 0; $j < $k; $j++) if ($next[$j] < $next[$toincrement]) $toincrement = $j; // Printing minimum in each iteration // print the value if output is not // equal to current value(to avoid // the duplicates) if ($output != $next[$toincrement]) { $output = $next[$toincrement]; echo $next[$toincrement] . " "; $i++; } // incrementing the current value // by the respective factor $next[$toincrement] += $factor[$toincrement]; } } // Driver code $factor = array(3, 5, 7); $n = 10; $k = count($factor); generateNumbers($factor, $n, $k); // This code is contributed by mits ?> |
Output :
3 5 6 7 9 10 12 14 15 18
Time complexity – O(n * k)
Auxiliary Space – O(k)
This article is contributed by Niteesh kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of all odd factors of numbers in the range [l, r]
- Sum of all even factors of numbers in the range [l, r]
- Common prime factors of two numbers
- Maximum factors formed by two numbers
- Count numbers from range whose prime factors are only 2 and 3
- K-Primes (Numbers with k prime factors) in a range
- Count common prime factors of two numbers
- Number of distinct prime factors of first n natural numbers
- Efficient program to print the number of factors of n numbers
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Find number of factors of N when location of its two factors whose product is N is given
- Find numbers which are multiples of first array and factors of second array
- Gaussian Filter Generation in C++
- No of Factors of n!



