Commit 9514fc1eb9dcaf17929ad2f3689aa10dce5b5432

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

p07: Fix numeros pares

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 10002
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 flgEnd = false;
24 24
25 do 25 do
26 { 26 {
27 27
28 flgEnd = false; 28 flgEnd = false;
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)
36 { 36 {
37 numerador = *ptrPrime; 37 numerador = *ptrPrime;
38 if (denominador % *ptrPrime++ == 0) 38 if (denominador % *ptrPrime++ == 0)
39 { 39 {
40 flgEnd = true; 40 flgEnd = true;
41 break; 41 break;
42 } 42 }
43 43
44 } 44 }
45 45
46 if( denominador % numerador == 0 && denominador == numerador) 46 if( denominador % numerador == 0 && denominador == numerador)
47 { 47 {
48 *ptrPrime = denominador; 48 *ptrPrime = denominador;
49 flgEnd = true; 49 flgEnd = true;
50 } 50 }
51 51
52 numerador++; 52 numerador++;
53 }while(!flgEnd); 53 }while(!flgEnd);
54 denominador++; 54 denominador++;
55 55
56 } while (arrayPrime[SIZE_ARRAY] == 0); 56 } while (arrayPrime[SIZE_ARRAY-1] == 0);
57 57
58 for (int i = 0; i < SIZE_ARRAY; ++i) 58 for (int i = 0; i < SIZE_ARRAY; ++i)
59 { 59 {
60 printf("%d\t-\t%d\n", i+1, arrayPrime[i-1]); 60 printf("%d\t-\t%d\n", i+1, arrayPrime[i]);
61 } 61 }
62 62
63 printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]); 63 printf("El primo #%d es:\t%d\n", SIZE_ARRAY, arrayPrime[SIZE_ARRAY-1]);
64 64
65 return 0; 65 return 0;
66 } 66 }