Commit a70faf338f2c23cd176f2334673779a804ba5d01

Authored by Francisco Javier Coutiño
1 parent 3826027976
Exists in master

p07: Optimizando para usar en problema 10.

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
1 /* 1 /*
2 10001st prime 2 10001st prime
3 Problem 7 3 Problem 7
4 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. 4 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
5 5
6 What is the 10 001st prime number? 6 What is the 10 001st prime number?
7 7
8 by: dbk 8 by: dbk
9 */ 9 */
10 10
11 #include <stdio.h> 11 #include <stdio.h>
12 12
13 #define SIZE_ARRAY 10001 13 #define SIZE_ARRAY 10001
14 14
15 int main(int argc, char const *argv[]) 15 int main(int argc, char const *argv[])
16 { 16 {
17 17
18 int *ptrPrime; 18 int *ptrPrime;
19 int numerador = 1; 19 int numerador = 1;
20 int denominador = 2; 20 int denominador = 2;
21 int arrayPrime[SIZE_ARRAY] = { 0 }; 21 int arrayPrime[SIZE_ARRAY] = { 0 };
22 22
23 int counter = 0;
24 int tmpCounter = 0;
25
23 bool flgBuscar = true; 26 bool flgBuscar = true;
24 27
25 do 28 do
26 { 29 {
27 30
28 flgBuscar = true; 31 flgBuscar = true;
29 numerador = 1; 32 numerador = 1;
30 ptrPrime = arrayPrime; 33 ptrPrime = arrayPrime;
31 34
32 do 35 do
33 { 36 {
34 37
35 while(*ptrPrime != 0 && flgBuscar) 38 while(*ptrPrime != 0 && flgBuscar)
36 { 39 {
37 numerador = *ptrPrime; 40 numerador = *ptrPrime;
38 flgBuscar = !(denominador % *ptrPrime++ == 0); 41 flgBuscar = !(denominador % *ptrPrime++ == 0);
39 42
40 } 43 }
41 44
42 if( denominador % numerador == 0 && denominador == numerador ) 45 if( denominador % numerador == 0 && denominador == numerador )
43 { 46 {
44 *ptrPrime = denominador; 47 *ptrPrime = denominador;
45 flgBuscar = false; 48 flgBuscar = false;
49 counter++;
46 50
47 } 51 }
48 else numerador++; 52 else numerador++;
49 53
50 }while(flgBuscar); 54 }while(flgBuscar);
51 55
52 denominador++; 56 denominador++;
53 57
58
54 } while (arrayPrime[SIZE_ARRAY-1] == 0); 59 } while (arrayPrime[SIZE_ARRAY-1] == 0);
55 60
56 printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]); 61 printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]);
57 62
58 return 0; 63 return 0;
59 } 64 }
07 - Primeros 10001 primos/tst
No preview for this file type
07 - Primeros 10001 primos/tstP07.cpp
File was created 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; }
106