I am given the prime factorization of a number p1^x1 * p2^x2 * .... in a map. I need to iterate through all its factors, prime as well as composite. I managed to write a solution using recursion.
#include <iostream>
#include <map>
#include <cstdlib>
using namespace std;
struct PROBLEM {
    int mx = 400;
    map<int, int> mp = {{2, 2}, {3, 1},  {5, 1}, {7, 2}};
    int lastPrimeFactor = 7;
    int num = 1;
    auto solve() {
        rec(2, 0);
        return 0;
    }
    int next_prime_factor(int p) {
        return (p == 2) ? 3 : (p == 3) ? 5 : (p == 5) ? 7 : -1;
    }
    void rec(int prime, int power) {
        if (mx == 0) {
            cout << "Infinite recursion\n\n";
            exit(0);
        } else --mx;
        if (prime == lastPrimeFactor && power > mp[prime]) {
            return;
        }
        if (power < mp[prime]) {
            num *= prime;
            cout << num << endl;
            rec(prime,  power + 1);
            num /= prime;
        }
        if (prime != lastPrimeFactor) {
            rec(next_prime_factor(prime),  0);
        }
    }
};
int main() {
    PROBLEM().solve();
    return 0;
}
Questions:
1) Is there any faster way to generate these factors?
2) If possible, can I replace the recursion by a while loop?
 
     
    