/* Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. */ #include #include #define SIZE_ARRAY 2000000 bool esPrimo(int eval, int *primosConocidos); int *lastAddr = 0; int main(int argc, char const *argv[]) { int arrayPrime[SIZE_ARRAY] = { 0 }; int counter; long sum = 0; bool continuar = true; //Agregando valores conocidos arrayPrime[0] = 2; arrayPrime[1] = 3; sum = 5; //Init counter = 3; do { counter+=2; sum += (esPrimo(counter, arrayPrime)) ? counter : 0; } while (counter < SIZE_ARRAY); printf("Sumatoria:\t%ld\n", sum); return 0; } bool esPrimo(int eval, int *primosConocidos) { bool valido = true; double res = 0; res = sqrt(eval); while(*primosConocidos != 0) { if((double) *primosConocidos <= res) { if ( eval % *primosConocidos == 0 ) { valido = false; break; } else { primosConocidos++; } } else { lastAddr = (lastAddr == 0) ? ++primosConocidos : lastAddr + 1; primosConocidos = lastAddr; *primosConocidos = eval; valido = true; break; } } return valido; }