Commit a70faf338f2c23cd176f2334673779a804ba5d01
1 parent
3826027976
Exists in
master
p07: Optimizando para usar en problema 10.
Showing
5 changed files
with
110 additions
and
0 deletions
Show diff stats
07 - Primeros 10001 primos/core
No preview for this file type
07 - Primeros 10001 primos/p07
No preview for this file type
07 - Primeros 10001 primos/p07.cpp
... | ... | @@ -20,6 +20,9 @@ int main(int argc, char const *argv[]) |
20 | 20 | int denominador = 2; |
21 | 21 | int arrayPrime[SIZE_ARRAY] = { 0 }; |
22 | 22 | |
23 | + int counter = 0; | |
24 | + int tmpCounter = 0; | |
25 | + | |
23 | 26 | bool flgBuscar = true; |
24 | 27 | |
25 | 28 | do |
... | ... | @@ -43,6 +46,7 @@ int main(int argc, char const *argv[]) |
43 | 46 | { |
44 | 47 | *ptrPrime = denominador; |
45 | 48 | flgBuscar = false; |
49 | + counter++; | |
46 | 50 | |
47 | 51 | } |
48 | 52 | else numerador++; |
... | ... | @@ -51,6 +55,7 @@ int main(int argc, char const *argv[]) |
51 | 55 | |
52 | 56 | denominador++; |
53 | 57 | |
58 | + | |
54 | 59 | } while (arrayPrime[SIZE_ARRAY-1] == 0); |
55 | 60 | |
56 | 61 | printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]); | ... | ... |
07 - Primeros 10001 primos/tst
No preview for this file type
07 - Primeros 10001 primos/tstP07.cpp
... | ... | @@ -0,0 +1,105 @@ |
1 | +#include <stdio.h> | |
2 | +#include <math.h> | |
3 | + | |
4 | +#define SIZE_ARRAY 1000000 | |
5 | + | |
6 | +bool esPrimo(int eval, int *primosConocidos); | |
7 | +bool esPar(int eval); | |
8 | + | |
9 | +int contadorPrimos = 0; | |
10 | + | |
11 | +int main(int argc, char const *argv[]) | |
12 | +{ | |
13 | + | |
14 | + int *ptrPrime; | |
15 | + int arrayPrime[SIZE_ARRAY] = { 0 }; | |
16 | + int counter; | |
17 | + int counterTmp=0; | |
18 | + | |
19 | + | |
20 | + bool valido; | |
21 | + | |
22 | + counter = 1; | |
23 | + | |
24 | + do | |
25 | + { | |
26 | + if (counter <= 2) | |
27 | + counter++; | |
28 | + else | |
29 | + counter+=2; | |
30 | + | |
31 | + valido = esPrimo(counter, arrayPrime); | |
32 | + | |
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); | |
42 | + | |
43 | + printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]); | |
44 | + | |
45 | + return 0; | |
46 | +} | |
47 | + | |
48 | + | |
49 | + | |
50 | +bool esPrimo(int eval, int *primosConocidos) | |
51 | +{ | |
52 | + bool valido = true; | |
53 | + double res = 0; | |
54 | + | |
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 | |
69 | + { | |
70 | + //printf("Test: SQRT\n"); | |
71 | + res = sqrt(eval); | |
72 | + while(*primosConocidos != 0) | |
73 | + { | |
74 | + if((double) *primosConocidos <= res) | |
75 | + { | |
76 | + //printf("Test: raiz menor\n"); | |
77 | + valido = (eval % *primosConocidos != 0); | |
78 | + } | |
79 | + else | |
80 | + { | |
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; | |
92 | + } | |
93 | + | |
94 | + if (!valido) break; | |
95 | + | |
96 | + primosConocidos++; | |
97 | + | |
98 | + } | |
99 | + | |
100 | + } | |
101 | + | |
102 | + return valido; | |
103 | +} | |
104 | + | |
105 | +bool esPar(int eval) { return eval % 2 == 0; } | ... | ... |