C ++ using multiple cores breakes output , code for a math problem

  c++, math, openmp

So, for start im just starting to learn c++, and i had to resolve this problem: Find the numbers with the propriety : 5 * 5 = 25 , 25 * 25 =625(25squared), 6*6 =36(6squared) ( 25 is the ending of 625, 5 is the the ending of 25 ). So i’ve got my code to find all the numbers lower than 30k , but then i wanted to push it to it’s limit ,so up to lluint_max, but it was really slow, i saw that my 12 core cpu is not utilised so i thought i’d add more cpu cores. I wanted to find the easiest fix and i found openmp, read a little and foun that if i add omp for it should divide the load to multiple cores, but the console doesn’t display my numbers anymore.(PS i enabled omp in vs)

Here’s my code:

#include <iostream>
#include <cmath>
#include <climits>
#include <omp.h>
using namespace std;
int main()
{
    long long int x, i, nc = 0, z = 1, p;
#pragma omp for
    for (x = 1; x <= ULLONG_MAX; x++)
    {

        //numarul de cifre a lui x
        while (x / z != 0)
        {
            z = z * 10;
            nc = nc + 1;
        }
        //patratul
        p = x * x;
        i = pow(10, nc);
        if (p % i == x)
            cout << x << endl;

    }
}

And heres my output:

C:UsersMihai CazacsourcereposConsoleAPP2DebugConsoleAPP2.exe (process 8736) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Expected output:

1
5
6
25
76
376
625
9376
90625
109376
890625
2890625
7109376
12890625
//and so on 

Thanks in advance!!

Source: Windows Questions C++

LEAVE A COMMENT