Blame view
10 - Sumatioria de primos/p10.cpp
1.15 KB
5a34e29d6 p10: Resuelto |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
/* 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 <stdio.h> #include <math.h> #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 ", 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; } |