Tanbin_Hasan's blog

By Tanbin_Hasan, history, 5 weeks ago, In English

https://www.geeksforgeeks.org/generating-all-divisors-of-a-number-using-its-prime-factorization/

Here , I got a answer for it . But I didn't understand the recursion process .

Could you please help me to do this iteratively ? Kindly convert it from recursive to iterative form . Need help from my programmer brothers . I'll be grateful to you all .

Thanks in advance .

 
 
 
 
  • Vote: I like it
  • -17
  • Vote: I do not like it

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Tanbin_Hasan (previous revision, new revision, compare).

»
5 weeks ago, # |
  Vote: I like it +17 Vote: I do not like it
int n;
cin >> n;
vector<pair<int, int>> primes;
for( int p = 2; p * p <= n; p++ ){
    if( n % p == 0 ){
        int cnt = 0;
        while( n % p == 0 ) cnt++, n /= p;
        primes.push_back( { p, cnt } );
    }
}

if( n > 1 ){
    primes.push_back( { n, 1 } );
}

vector<int> d;
d.push_back( 1 );

for( int i = 0; i < primes.size(); i++ ){
    int size = d.size();
    int p_pow = 1;
    for( int j = 0; j < primes[i].second; j++ ){
        p_pow *= primes[i].first;
        for( int i = 0; i < size; i++ ){
            d.push_back( d[i] * p_pow );
        }
    }
}

for( int x: d ){
    cout << x << " ";
}
cout << endl;