Commit fa2c5b27d6b5919b792e973f14e8b1f206ce3ec7

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

Agregando problemas resueltos

01 - Multiplos 3 y 5/p01
No preview for this file type
01 - Multiplos 3 y 5/p01.cpp
File was created 1 /*
2
3 Multiples of 3 and 5
4 Problem 1
5 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
6 Find the sum of all the multiples of 3 or 5 below 1000.
7
8 By: dbk
9
10 */
11
12 #include <iostream>
13 #include <stdio.h>
14
15 using namespace std;
16
17 int main()
18 {
19 int res = 0;
20
21 for (int i = 1; i < 1000; i++)
22 {
23 res += (i % 3 == 0 || i % 5 == 0) ? i : 0;
24 }
25
26 printf("Resultado: %i\n", res);
27
28 return 0;
29 }
02 - Serie Fibonacci/p02
No preview for this file type
02 - Serie Fibonacci/p02.cpp
File was created 1 /*
2 Even Fibonacci numbers
3 Problem 2
4 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
5
6 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
7
8 By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
9
10 by:dbk
11
12 */
13
14 #include <stdio.h>
15
16 #define LIMITE 4000000
17
18 int main()
19 {
20
21 int i;
22 unsigned int a = 1;
23 unsigned int b = 1;
24 unsigned int res = 0;
25 unsigned int tmp = 0;
26
27 do
28 {
29
30 tmp = a;
31 a = b;
32 b += tmp;
33
34 if (b % 2 == 0) res += b;
35
36 }while(b < LIMITE);
37
38 printf("\nResultado final: %ld\n", res);
39
40 return 0;
41 }
03 - Factor primo/p03
No preview for this file type
03 - Factor primo/p03.cpp
File was created 1 /*
2
3 Largest prime factor
4 Problem 3
5 Published on Friday, 2nd November 2001, 12:00 pm; Solved by 256800
6 The prime factors of 13195 are 5, 7, 13 and 29.
7
8 What is the largest prime factor of the number 600851475143 ?
9
10 By: dbk
11
12 */
13 #include <stdio.h>
14
15 #define CALCULAR 600851475143
16 #define MAX 1000000
17
18 int main(int argc, char const *argv[])
19 {
20
21 long res = 0;
22 long valor = CALCULAR;
23
24 for (int i = 2; i < MAX; i++)
25 {
26 while ( valor % i == 0)
27 {
28 valor /= i;
29 res = i;
30 }
31
32 }
33
34 printf("Mayor factor primo: %ld\n", res);
35
36 return 0;
37 }
04 - Producto palindromo/itoaEjemplo.cpp
File was created 1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main ()
5 {
6 int i;
7 char buffer [33];
8 printf ("Enter a number: ");
9 scanf ("%d",&i);
10 itoa (i,buffer,10);
11 printf ("decimal: %s\n",buffer);
12 itoa (i,buffer,16);
13 printf ("hexadecimal: %s\n",buffer);
14 itoa (i,buffer,2);
15 printf ("binary: %s\n",buffer);
16 return 0;
17 }
04 - Producto palindromo/p04
No preview for this file type
04 - Producto palindromo/p04.cpp
File was created 1 /*
2 Largest palindrome product
3 Problem 4
4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
5
6 Find the largest palindrome made from the product of two 3-digit numbers.
7
8 By:dbk
9 */
10 #include <stdio.h>
11 #include <string.h>
12
13 using namespace std;
14
15 #define MAX 10000
16
17 bool validPalindromic(int);
18 bool validPalindromicApuntador(int);
19
20 int main(int argc, char const *argv[])
21 {
22
23 int a = 1;
24 int b = 1;
25
26 int c = 0;
27
28 int res = 0;
29
30 do
31 {
32 a = 1;
33 do
34 {
35 c = a * b;
36 res = (validPalindromicApuntador(c) && c > res) ? c : res;
37
38 }while(a++ < MAX);
39 }while(b++ < MAX);
40 printf("Resultado final: %d\n", res);
41 return 0;
42 }
43
44
45 bool validPalindromic(int valueCheck)
46 {
47 bool flagValid = true;
48 char buffer[30];
49 int counterDsc = 0;
50 int counterAsc = 0;
51 int sizeStringNumber = 0;
52
53 //Convierte el entero a una cadena
54 sprintf(buffer, "%d", valueCheck);
55 counterDsc = sizeStringNumber = strlen(buffer);
56 while(sizeStringNumber > counterAsc) {
57 flagValid &= (buffer[counterAsc++] == buffer[--counterDsc]) ? true : false;
58 }
59
60 return flagValid;
61 }
62
63 bool validPalindromicApuntador(int valueCheck)
64 {
65 char *ptrAsc;
66 char *ptrDsc;
67 char buffer[30];
68 bool flagValid = true;
69
70 //Convierte el entero a una cadena
71 sprintf(buffer, "%d", valueCheck);
72 ptrAsc = buffer;
73 ptrDsc = &buffer[strlen(buffer)];
74
75 while(ptrDsc >= ptrAsc) {
76 flagValid &= (*(ptrAsc++) == *(--ptrDsc));
77 }
78 return flagValid;
79 }
80
81 /*
82 TEST DIR MEM Apuntadores
83 printf("mem1:\t%p\n", ptrAsc);
84 printf("mem2:\t%p\n", ptrDsc);
85 */
05 - Multiplo menor/p05
No preview for this file type
05 - Multiplo menor/p05.cpp
File was created 1 /*
2 Smallest multiple
3
4 Problem 5
5 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
6
7 What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
8 */
9 #include <stdio.h>
10
11 using namespace std;
12
13 #define MAX_VALUE 20
14
15
16 int main(int argc, char const *argv[])
17 {
18 int i = 0;
19 int incMultiplos = 0;
20
21 bool flgValid = true;
22
23 do
24 {
25 i++;
26 flgValid = true;
27 incMultiplos = MAX_VALUE;
28
29 while(incMultiplos > 0)
30 {
31 flgValid &= (i % incMultiplos-- == 0);
32 if (!flgValid) break;
33 }
34
35 }while(!flgValid);
36
37 printf("i: %d \t%x\n", i, flgValid);
38
39 return 0;
40 }
06 - Suma de diferencia de cuadrados/p06
No preview for this file type
06 - Suma de diferencia de cuadrados/p06.cpp
File was created 1 /*
2
3 Sum square difference
4 Problem 6
5 The sum of the squares of the first ten natural numbers is,
6
7 12 + 22 + ... + 102 = 385
8 The square of the sum of the first ten natural numbers is,
9
10 (1 + 2 + ... + 10)2 = 552 = 3025
11 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
12
13 Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
14
15 */
16
17 #include <stdio.h>
18 #include <math.h>
19
20 using namespace std;
21
22 #define LIMITE_RANGO 100
23
24 int main(int argc, char const *argv[])
25 {
26
27 int i = 0;
28 int res = 0;
29 int valSum = 0;
30 int valPow = 0;
31
32 while(i++ < LIMITE_RANGO)
33 {
34 valSum += i;
35 valPow += pow(i, 2);
36 }
37
38 res = pow(valSum, 2) - valPow;
39 printf("Resultado final: %d\n", res);
40
41 return 0;
42 }
File was created 1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 res = 0;
8
9 for (int i = 10; i > 0; i--)
10 {
11
12 if (i % 3 == 0 || i % 5)
13 {
14 res += i;
15 }
16
17 }
18
19 cout << res << endl;
20
21 }