STL issue

I had come over another issue with STL.
I am trying to write a program that takes a number 'n' (1 <= n <= 10.000) and prints the "decomposition into prime factors" (I know that the translation to English isn't good).

I have written the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <map>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    typedef pair<int, int> TIntPair;

    ifstream iFILE("numar.in");
    ofstream oFILE("factorial.out");

    map<int, int> m;
    map<int, int>::iterator mIt;

    int n;

    if (iFILE && !iFILE.eof() && iFILE >> n)
    {
        for (int k = 2; k <= n; k++)
        {
            int aux = k;
            for (int i = 2; aux > 1 && i <= k; ++i)
            {
                while (aux % i == 0)
                {
                    if (m.find(i) == m.end())
                        m.insert(TIntPair(i,1));
                    else
                    {
                        mIt = m.find(i);
                        ++(mIt->second);
                    }
                }
                aux /= i;
            }
        }
    }

    for (mIt = m.begin(); mIt != m.end(); mIt++)
    {
        oFILE << mIt->first << "^";
        oFILE << mIt->second << endl;
    }

    return 0;
}


Example input/output:

numar.in

45


factorial.out
2^41
3^21
5^10
7^6
11^4
13^3
17^2
19^2
23^1
29^1
31^1
37^1
41^1
43^1


The program doesn't show any errors or warnings at compilation, but it looks like it gets stuck or goes into an infinite loop, as it doesn't show any message in the console window and neither does it modify the output file.

Thanks in advance.
Your translation is spot-on.

You have an infinite loop starting at line 25, and your OS is too stupid to notice and kill the program for you.


Either way though, you need to rethink how you are decomposing n into prime factors. Your output, for example, should be

    32 × 51

since 3 × 3 × 5 = 45.


Using the map is smart. The way you are checking for primes and factors is confused. As you are figuring it out, remember that it is OK to modify n. Hint:

1
2
3
4
5
6
7
8
9
10
11
12
for every prime number p from 2 to (n / 2) AND while (n != 0)
{
    if p evenly divides n
    {
        update the map with this simple code:
            m[ p ] += 1;
        update n:
            n /= p;
    }
}

write the output using lines 40 through 44 as you have it already

Good luck!

[edit] fixing stuff my brain left out...
Last edited on
Firstly, I guess you'd be amazed to find out that my OS is the brand new Windows 8.
Thanks for the reply and for the explanation.
I think I will be able to work it out now.

EDIT: About the text... Yeah, sorry, the program is suppose to show the "decomposition into prime factors" of the n! (n factorial). So, in my example, 1 x 2 x 3 x ... x 45

EDIT 2: Found my issue... The line 35 should have been before line 34, so the program works now. Thanks again. ^^

Best of wishes,
~ Raul ~
Last edited on
Topic archived. No new replies allowed.