Commit f47e92536f5af15e691e4b0a3c020248efbe30eb
1 parent
a70faf338f
Exists in
master
p07: Listo
Showing
2 changed files
with
23 additions
and
55 deletions
Show diff stats
07 - Primeros 10001 primos/tst
No preview for this file type
07 - Primeros 10001 primos/tstP07.cpp
| 1 | #include <stdio.h> | 1 | #include <stdio.h> |
| 2 | #include <math.h> | 2 | #include <math.h> |
| 3 | 3 | ||
| 4 | #define SIZE_ARRAY 1000000 | 4 | #define SIZE_ARRAY 10001 |
| 5 | 5 | ||
| 6 | bool esPrimo(int eval, int *primosConocidos); | 6 | bool esPrimo(int eval, int *primosConocidos); |
| 7 | bool esPar(int eval); | 7 | bool esPar(int eval); |
| 8 | 8 | ||
| 9 | int contadorPrimos = 0; | 9 | int *lastAddr = 0; |
| 10 | 10 | ||
| 11 | int main(int argc, char const *argv[]) | 11 | int main(int argc, char const *argv[]) |
| 12 | { | 12 | { |
| 13 | |||
| 14 | int *ptrPrime; | ||
| 15 | int arrayPrime[SIZE_ARRAY] = { 0 }; | 13 | int arrayPrime[SIZE_ARRAY] = { 0 }; |
| 16 | int counter; | 14 | int counter; |
| 17 | int counterTmp=0; | ||
| 18 | |||
| 19 | 15 | ||
| 20 | bool valido; | 16 | bool valido; |
| 21 | 17 | ||
| 22 | counter = 1; | 18 | //Agregando valores conocidos |
| 19 | arrayPrime[0] = 2; | ||
| 20 | arrayPrime[1] = 3; | ||
| 23 | 21 | ||
| 22 | //Init | ||
| 23 | counter = 3; | ||
| 24 | do | 24 | do |
| 25 | { | 25 | { |
| 26 | if (counter <= 2) | 26 | counter+=2; |
| 27 | counter++; | ||
| 28 | else | ||
| 29 | counter+=2; | ||
| 30 | |||
| 31 | valido = esPrimo(counter, arrayPrime); | 27 | valido = esPrimo(counter, arrayPrime); |
| 32 | 28 | ||
| 33 | |||
| 34 | if(counterTmp != contadorPrimos && contadorPrimos % 100000 == 0) | ||
| 35 | { | ||
| 36 | counterTmp = contadorPrimos; | ||
| 37 | printf("%d\t-\t%d\n", counter, contadorPrimos); | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | } while (arrayPrime[SIZE_ARRAY-1] == 0); | 29 | } while (arrayPrime[SIZE_ARRAY-1] == 0); |
| 42 | 30 | ||
| 43 | printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]); | 31 | printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]); |
| 44 | 32 | ||
| 45 | return 0; | 33 | return 0; |
| 46 | } | 34 | } |
| 47 | 35 | ||
| 48 | 36 | ||
| 49 | |||
| 50 | bool esPrimo(int eval, int *primosConocidos) | 37 | bool esPrimo(int eval, int *primosConocidos) |
| 51 | { | 38 | { |
| 52 | bool valido = true; | 39 | bool valido = true; |
| 53 | double res = 0; | 40 | double res = 0; |
| 54 | 41 | ||
| 55 | if(eval == 2) | 42 | res = sqrt(eval); |
| 56 | { | 43 | while(*primosConocidos != 0) |
| 57 | //printf("Test: 2\n"); | ||
| 58 | *primosConocidos = 2; | ||
| 59 | valido = true; | ||
| 60 | } | ||
| 61 | /* | ||
| 62 | else if(esPar(eval)) | ||
| 63 | { | ||
| 64 | //printf("Test: par\n"); | ||
| 65 | valido = false; | ||
| 66 | } | ||
| 67 | */ | ||
| 68 | else | ||
| 69 | { | 44 | { |
| 70 | //printf("Test: SQRT\n"); | 45 | if((double) *primosConocidos <= res) |
| 71 | res = sqrt(eval); | ||
| 72 | while(*primosConocidos != 0) | ||
| 73 | { | 46 | { |
| 74 | if((double) *primosConocidos <= res) | 47 | if ( eval % *primosConocidos == 0 ) |
| 75 | { | 48 | { |
| 76 | //printf("Test: raiz menor\n"); | 49 | valido = false; |
| 77 | valido = (eval % *primosConocidos != 0); | 50 | break; |
| 78 | } | 51 | } |
| 79 | else | 52 | else |
| 80 | { | 53 | { |
| 81 | //printf("Test: raiz mayor\n"); | 54 | primosConocidos++; |
| 82 | |||
| 83 | while(*primosConocidos != 0) { | ||
| 84 | primosConocidos++; | ||
| 85 | } | ||
| 86 | |||
| 87 | //printf("%d\t", eval); | ||
| 88 | contadorPrimos++; | ||
| 89 | *primosConocidos = eval; | ||
| 90 | valido = true; | ||
| 91 | break; | ||
| 92 | } | 55 | } |
| 56 | } | ||
| 57 | else | ||
| 58 | { | ||
| 93 | 59 | ||
| 94 | if (!valido) break; | 60 | lastAddr = (lastAddr == 0) ? ++primosConocidos : lastAddr + 1; |
| 95 | |||
| 96 | primosConocidos++; | ||
| 97 | 61 | ||
| 62 | primosConocidos = lastAddr; | ||
| 63 | *primosConocidos = eval; |