Project Euler #12 in C++ - highly divisible triangular number Project Euler Problem 12 asks (paraphrased):


  The nth triangular number Tn = 1 + 2 + … + n.  T7 = 28 has 6 divisors (1, 2, 4, 7, 14, 28).  What is the first Tn to have over 500 divisors?


After asking for some help on Stack Overflow, I've managed to write this code to solve it:

#include "stdafx.h"
#include &lt;iostream&gt;
#include &lt;map&gt;
//#include &lt;algorithm&gt;
#include &lt;numeric&gt;

int divisors (unsigned int num, std::map&lt;int, int&gt;&amp; map) 
{
    int orig_num = num;
    for(unsigned int i = 2; i &lt;= num; ++i)
    {
        while (num % i == 0) 
        {
            num /= i;
            ++map[i];
            //std::cout &lt;&lt; map[i] &lt;&lt; "\t";
        }
    }
    std::cout &lt;&lt; orig_num &lt;&lt; " = "; 
    for(auto&amp; iter:map)
        std::cout &lt;&lt; iter.first &lt;&lt;  "^" &lt;&lt; iter.second &lt;&lt; " * ";
    return 0;
}

int overall_factors(unsigned int n)
{
    std::map &lt;int, int&gt; primefactors1, primefactors2; 
    int no_of_divisors = 1;
    divisors(n,primefactors1);
    std::cout &lt;&lt; std::endl;
    divisors(n+1,primefactors2);
    std::cout &lt;&lt; std::endl;
    std::map&lt;int, int&gt; primefactors_triangle = std::accumulate( primefactors1.begin(), primefactors1.end(), std::map&lt;int, int&gt;(),
        []( std::map&lt;int, int&gt; &amp;m, const std::pair&lt;const int, int&gt; &amp;p )
    {
        return ( m[p.first] +=p.second, m );
    } );

    primefactors_triangle = std::accumulate( primefactors2.begin(), primefactors2.end(), primefactors_triangle,
        []( std::map&lt;int, int&gt; &amp;m, const std::pair&lt;const int, int&gt; &amp;p )
    {
        return ( m[p.first] +=p.second, m );
    } );

    for ( const auto &amp;p : primefactors_triangle )
    {
        std::cout &lt;&lt; "{ " &lt;&lt; p.first &lt;&lt; ", " &lt;&lt; p.second &lt;&lt; " } ";
        if (p.first == 2)
            no_of_divisors *= p.second;
        else
        no_of_divisors *= p.second+1;
    }

    std::cout &lt;&lt; std::endl;
    return no_of_divisors;
}

int main()
{
    int i = 2;
    while (overall_factors(i) &lt; 500)
    {
        std::cout &lt;&lt; overall_factors(i) &lt;&lt; std::endl;
        ++i;
    }
    return 0;
}


There're some pieces left that output debugging info to see that everything is working as intended. But overall, it doesn't work as fast as I want it to - or as fast as I expect a proper solution to a Project Euler problem to work.

How can this code be improved?  Clearly the program, as it is, now calculates primefactors for a given number twice.  What else is wrong and could be improved? 

UPD: After I changed the while loop condition, the problem was solved rather quickly. Still, I believe my code is improvable.
This:

int i = 2;
while (overall_factors(i) &lt; 500)
{
    std::cout &lt;&lt; overall_factors(i) &lt;&lt; std::endl;
    ++i;
}


can just be a for-loop:

for (int i = 2; overall_factors(i) &lt; 500; ++i)
{
    std::cout &lt;&lt; overall_factors(i) &lt;&lt; std::endl;
}


I'd also recommend using a constant for 500 as it is otherwise a magic number.  This will make it clear as to what this value represents.

If the number isn't significant to the general algorithm, then a comment can be added instead.
There is no need for divisors to just return a 0.  It appears it's just modifying the map and displaying it.  Also, these are two different things, and a function should just have one role.  In this case, it should just modify the map.  You should then have a separate void function for displaying the map in the desired format.

Although passing the map by reference is okay here, passing it by value (in C++11) will also allow the compiler to decide to perform a move, if desired over a copy.