Brute force solution to Project Euler Problem #52 I was solving Project Euler Problem #52:

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

in C++, and this is the code I used:
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

bool checkCondition(long long int number);

int main()
{
    long long int num = 1;
    while (!checkCondition(num))
    {
        ++num;
    }

    std::cout &lt;&lt; num &lt;&lt; &quot;\n&quot;;
}

bool checkCondition(long long int number)
{
    std::string numString = std::to_string(number);
    std::string numx2 = std::to_string(number * 2);
    std::string numx3 = std::to_string(number * 3);
    std::string numx4 = std::to_string(number * 4);
    std::string numx5 = std::to_string(number * 5);
    std::string numx6 = std::to_string(number * 6);

    return std::is_permutation(numString.begin(), numString.end(), numx2.begin()) &amp;&amp;
        std::is_permutation(numString.begin(), numString.end(), numx3.begin()) &amp;&amp;
        std::is_permutation(numString.begin(), numString.end(), numx4.begin()) &amp;&amp;
        std::is_permutation(numString.begin(), numString.end(), numx5.begin()) &amp;&amp;
        std::is_permutation(numString.begin(), numString.end(), numx6.begin());
}

As evident, this function uses the standard library function std::is_permutation and literally iterates over every single integer and converts them to strings to find the answer, which is obviously inefficient. How can I improve upon the efficiency of this program?
Before I begin: I solved this particular Project Euler problem in 10 seconds without even writing a program, because I had seen a Numberphile video on this concept and I had memorized the number. It's really just a mathematical problem!

Whenever you must do brute force, you have two primary goals:


Reduce the number of trials by eliminating some definitely incorrect cases. In this example, if N and N * 6 doesn't have the same number of digits, they cannot be the permutations of each other. In our case this won't be much useful, but if you did some maths, you would notice that you would only have to check numbers between 100000..~170000, 200000..~270000, because numbers outside those ranges don't satisfy the condition stated above. If we were asked to find a much larger solution, using this approach would have eliminated about 30% of the options right away.
Reduce the amount of effort you must give at each trial. In this case, if 2 * N isn't a permutation of N, you shouldn't check for 3 * N because you know that you aren't getting a solution.


Finally, if I were you, I wouldn't use string operations. You can convert each number into a sorted list of digits in it, and then check for list equality.

Combining all of the above, you could write something like this:

A = sorted list of digits in (current trial)
for i = 2 to 6 inclusive:
    B = sorted list of digits in (current trial times i)
    if A and B are different, fail
succeed! -- can only reach here if all former multiples are permutations