Find the sum of prime numbers below 2 million This code is trying to find the sum of prime numbers below 2 million. The code does work as intended, but it takes a very long time ( 6 minutes) for the code to execute completely. What is the problem and how can I make it more efficient?

#include &lt;iostream&gt;
using namespace std;

unsigned long summationOfPrime();
bool isPrime(int n);
int main() {

cout &lt;&lt;summationOfPrime() &lt;&lt; endl;

}

unsigned long summationOfPrime()
{
const int num = 2000000;
unsigned long sum = 0;
for(int i = 2; i &lt; num; i++)
{
    if(isPrime(i) == true)
    {
        sum += i;

    }
}

return sum;
}

bool isPrime(int n)
 {
// Corner case
if (n &lt;= 1)
    return false;

// Check from 2 to n-1
for (int i = 2; i &lt; n; i++)
    if (n % i == 0)
        return false;

return true;
}
There are several issues, but here goes. Firstly, your indentation is all wonky. Some of this may be due to trying to input stuff here, but either way, indentation helps legibility.

Next, the biggest thing you can change to speed thing up is to replace for (int i = 2; i &lt; n; i++) with for (int i = 2; i*i &lt;= n; i++). This works because if \$a \cdot b = n\$, one of them is at most the square root of \$n\$. This change will probably make your code fast enough on its own.

That said, the much better solution to this type of problem is to use a prime sieve such as the Sieve of Eratosthenes which will generate all of the primes less than 1,000,000 faster by using addition and multiplication instead of modulo operations which are slower.