commit 5fb12b2ec35b56614167a55d9373bb21a01c5d26
parent 91b7f4419bd470ea646174f738d42227a8c1b73a
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Mon, 1 Apr 2024 21:29:25 -0300
Add `guia 3` solutions
Diffstat:
7 files changed, 127 insertions(+), 0 deletions(-)
diff --git a/guias/3/ej01.cpp b/guias/3/ej01.cpp
@@ -0,0 +1,13 @@
+#include <iostream>
+
+unsigned int factorial_r(unsigned int n) {
+ if((n == 0) || (n == 1))
+ return 1;
+
+ return n*factorial_r(n-1);
+}
+
+int main (void) {
+ std::cout << factorial_r(5) << std::endl;
+ return 0;
+}
diff --git a/guias/3/ej02.cpp b/guias/3/ej02.cpp
@@ -0,0 +1,18 @@
+#include <iostream>
+
+// If called without an exponent value it returns the number squared
+int pow_r(int x) {
+ return x*x;
+}
+
+int pow_r(int x, int y) {
+ if(y == 0)
+ return 1;
+
+ return x*pow_r(x, y-1);
+}
+
+int main (void) {
+ std::cout << pow_r(2, 8) << std::endl;
+ return 0;
+}
diff --git a/guias/3/ej03.cpp b/guias/3/ej03.cpp
@@ -0,0 +1,13 @@
+#include <iostream>
+
+int product_r(int x, int y) {
+ if(!x || !y)
+ return 0;
+
+ return (y < 0) ? -product_r(x, -y) : x+product_r(x, y-1);
+}
+
+int main (void) {
+ std::cout << product_r(-1, -1) << std::endl;
+ return 0;
+}
diff --git a/guias/3/ej04.cpp b/guias/3/ej04.cpp
@@ -0,0 +1,14 @@
+// https://en.wikipedia.org/wiki/Fibonacci_sequence
+
+#include <iostream>
+
+int fibonacci_r(int x) {
+ return (x <= 1) ? x : fibonacci_r(x-1) + fibonacci_r(x-2);
+}
+
+int main (void) {
+ for(size_t i = 0; i <= 10; ++i)
+ std::cout << fibonacci_r(i) << std::endl;
+
+ return 0;
+}
diff --git a/guias/3/ej05.cpp b/guias/3/ej05.cpp
@@ -0,0 +1,18 @@
+// Taken from /guias/1/ej27.cpp
+// https://en.wikipedia.org/wiki/Greatest_common_divisor
+
+#include <iostream>
+
+int find_gcd(int a, int b) {
+ if(!b)
+ return a;
+
+ return find_gcd(b, a % b);
+}
+
+
+int main (void) {
+ std::cout << find_gcd(8, 12) << std::endl;
+
+ return 0;
+}
diff --git a/guias/3/ej06.cpp b/guias/3/ej06.cpp
@@ -0,0 +1,20 @@
+// https://en.wikipedia.org/wiki/Ackermann_function
+
+#include <iostream>
+
+unsigned int ackerman(unsigned int m, unsigned int n) {
+ if(!m)
+ return n + 1;
+
+ if(!n)
+ return ackerman(m-1, 1);
+
+ return ackerman(m-1, ackerman(m, n-1));
+}
+
+
+int main (void) {
+ std::cout << ackerman(3, 3) << std::endl;
+
+ return 0;
+}
diff --git a/guias/3/ej07.cpp b/guias/3/ej07.cpp
@@ -0,0 +1,31 @@
+// https://en.wikipedia.org/wiki/Tower_of_Hanoi
+
+#include <iostream>
+
+// Gives the steps to follow in order to move all the disks from src to dst
+void move_stack(int disks, int src, int dst, int tmp)
+{
+ // Base case, move one disk from src to dst
+ if(disks == 1) {
+ printf("%d -> %d\n", src, dst);
+ return;
+ }
+
+ // Move every disk except the biggest one to the tmp peg
+ move_stack(disks - 1, src, tmp, dst);
+
+ // Move the biggest one to the dst peg
+ move_stack(1, src, dst, tmp);
+
+ // Move all the disks on the tmp peg to the dst one
+ move_stack(disks - 1, tmp, dst, src);
+ return;
+}
+
+int main (void) {
+ // Move 3 disks from first peg to third peg
+ // using the second peg as temporary
+ move_stack(3, 1, 3, 2);
+
+ return 0;
+}