0
I’m doing a program that adds the amount of prime numbers from 1 to 2000,000. When trying to compile, it returns that there was exceeded runtime. I would like to know ways to optimize the time of my program.
#include <stdio.h>
int main()
{
long long int i = 0, j = 0, count = 0, soma = 0;
for (j = 1; j <= 2000000; j++, count = 0)
{
for (i = 1; i <= 2000000; i++)
{
if (j % i == 0)
{
count++;
}
}
if (count == 2)
{
soma += + j;
}
}
printf("%lld\n", soma);
return 0;
}
What you’re looking for is this How to generate 200,000 primes as fast as possible in Python? using the Sieve of Eratosthenes which is the detail.
– Isac