Commit 3826027976b10576571d106b9e77f1f7b1825865

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

p07: Reduccion codigo...

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 10002 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 bool flgEnd = false; 23 bool flgBuscar = true;
24 24
25 do 25 do
26 { 26 {
27 27
28 flgEnd = false; 28 flgBuscar = true;
29 numerador = 1; 29 numerador = 1;
30 ptrPrime = arrayPrime; 30 ptrPrime = arrayPrime;
31 31
32 do 32 do
33 { 33 {
34 34
35 while(*ptrPrime != 0) 35 while(*ptrPrime != 0 && flgBuscar)
36 { 36 {
37 numerador = *ptrPrime; 37 numerador = *ptrPrime;
38 if (denominador % *ptrPrime++ == 0) 38 flgBuscar = !(denominador % *ptrPrime++ == 0);
39 {
40 flgEnd = true;
41 break;
42 }
43 39
44 } 40 }
45 41
46 if( denominador % numerador == 0 && denominador == numerador) 42 if( denominador % numerador == 0 && denominador == numerador )
47 { 43 {
48 *ptrPrime = denominador; 44 *ptrPrime = denominador;
49 flgEnd = true; 45 flgBuscar = false;
46
50 } 47 }
48 else numerador++;
49
50 }while(flgBuscar);
51 51
52 numerador++;
53 }while(!flgEnd);
54 denominador++; 52 denominador++;
55 53
56 } while (arrayPrime[SIZE_ARRAY-1] == 0); 54 } while (arrayPrime[SIZE_ARRAY-1] == 0);
57 55
58 for (int i = 0; i < SIZE_ARRAY; ++i)
59 {
60 printf("%d\t-\t%d\n", i+1, arrayPrime[i]);
61 }
62