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 | 1 | #include <stdio.h> |
2 | 2 | #include <math.h> |
3 | 3 | |
4 | -#define SIZE_ARRAY 1000000 | |
4 | +#define SIZE_ARRAY 10001 | |
5 | 5 | |
6 | 6 | bool esPrimo(int eval, int *primosConocidos); |
7 | 7 | bool esPar(int eval); |
8 | 8 | |
9 | -int contadorPrimos = 0; | |
9 | +int *lastAddr = 0; | |
10 | 10 | |
11 | 11 | int main(int argc, char const *argv[]) |
12 | 12 | { |
13 | - | |
14 | - int *ptrPrime; | |
15 | 13 | int arrayPrime[SIZE_ARRAY] = { 0 }; |
16 | 14 | int counter; |
17 | - int counterTmp=0; | |
18 | - | |
19 | 15 | |
20 | 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 | 24 | do |
25 | 25 | { |
26 | - if (counter <= 2) | |
27 | - counter++; | |
28 | - else | |
29 | - counter+=2; | |
30 | - | |
26 | + counter+=2; | |
31 | 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 | 29 | } while (arrayPrime[SIZE_ARRAY-1] == 0); |
42 | 30 | |
43 | 31 | printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]); |
... | ... | @@ -46,55 +34,35 @@ int main(int argc, char const *argv[]) |
46 | 34 | } |
47 | 35 | |
48 | 36 | |
49 | - | |
50 | 37 | bool esPrimo(int eval, int *primosConocidos) |
51 | 38 | { |
52 | 39 | bool valido = true; |
53 | 40 | double res = 0; |
54 | 41 | |
55 | - if(eval == 2) | |
56 | - { | |
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 | |
42 | + res = sqrt(eval); | |
43 | + while(*primosConocidos != 0) | |
69 | 44 | { |
70 | - //printf("Test: SQRT\n"); | |
71 | - res = sqrt(eval); | |
72 | - while(*primosConocidos != 0) | |
45 | + if((double) *primosConocidos <= res) | |
73 | 46 | { |
74 | - if((double) *primosConocidos <= res) | |
47 | + if ( eval % *primosConocidos == 0 ) | |
75 | 48 | { |
76 | - //printf("Test: raiz menor\n"); | |
77 | - valido = (eval % *primosConocidos != 0); | |
49 | + valido = false; | |
50 | + break; | |
78 | 51 | } |
79 | 52 | else |
80 | 53 | { |
81 | - //printf("Test: raiz mayor\n"); | |
82 | - | |
83 | - while(*primosConocidos != 0) { | |
84 | - primosConocidos++; | |
85 | - } | |
86 | - | |
87 | - //printf("%d\t", eval); | |
88 | - contadorPrimos++; | |
89 | - *primosConocidos = eval; | |
90 | - valido = true; | |
91 | - break; | |
54 | + primosConocidos++; | |
92 | 55 | } |
56 | + } | |
57 | + else | |
58 | + { | |
93 | 59 | |
94 | - if (!valido) break; | |
95 | - | |
96 | - primosConocidos++; | |
60 | + lastAddr = (lastAddr == 0) ? ++primosConocidos : lastAddr + 1; | |
97 | 61 | |
62 | + primosConocidos = lastAddr; | |
63 | + *primosConocidos = eval; | |
64 | + valido = true; | |
65 | + break; | |
98 | 66 | } |
99 | 67 | |
100 | 68 | } | ... | ... |