← Volver a 03_Unit_Test_Calculator
↑ Volver a inicio / Back to home
| Español | English |
|---|---|
| Explorar la optimización de código implementando algoritmos numéricos clásicos (suma de primeros n números, factorial, Fibonacci, máximo común divisor, mínimo común múltiplo) en tres enfoques progresivos: recursión directa, recursión con acumulador (tail-call) e iterativo, comprendiendo las ventajas de cada uno en términos de legibilidad, eficiencia y uso de memoria. | Explore code optimization by implementing classic numerical algorithms (sum of first n numbers, factorial, Fibonacci, greatest common divisor, least common multiple) in three progressive approaches: direct recursion, accumulator recursion (tail-call), and iterative, understanding the advantages of each in terms of readability, efficiency, and memory usage. |
| Español | English |
Crear un proyecto numbers con un único archivo que implemente los algoritmos de suma de los primeros n números, factorial, Fibonacci, máximo común divisor y mínimo común múltiplo en tres enfoques (recursión directa _rec, recursión con acumulador _acc e iterativo _ite), más helpers privados _help. Incluir suites de prueba unitarias separadas por enfoque que verifiquen todas las implementaciones con los casos de prueba especificados. |
Create a numbers project with a single file implementing the sum of first n numbers, factorial, Fibonacci, greatest common divisor, and least common multiple algorithms in three approaches (direct recursion _rec, accumulator recursion _acc, and iterative _ite), plus private _help helpers. Include separate unit test suites per approach that verify all implementations with the specified test cases. |
| Módulo | Enfoque | Características |
|---|---|---|
recursive |
Recursión directa (definición matemática) | ❌ Múltiples llamadas recursivas, alto uso de pila, código más legible y cercano a la definición matemática |
recursive_with_accumulator |
Recursión con acumulador (tail-call style) | ✅ Una sola llamada recursiva por paso, prepara el terreno para la versión iterativa, menor uso de pila (si el lenguaje implementa tail-call optimization) |
iterative |
Iterativo (bucles) | ✅✅ Sin llamadas recursivas, memoria constante, máxima eficiencia en tiempo y espacio |
Ninguna (no requiere entrada del usuario; las pruebas definen sus propios valores). None (no user input required; tests define their own values).
tests runned 33
passed 33
failed 0
ES: La salida exacta depende del framework/biblioteca de pruebas del lenguaje, pero todas las pruebas deben pasar (failed = 0).
EN: The exact output depends on the language’s test framework/library, but all tests must pass (failed = 0).
recursive usa recursión directa (similar a la definición matemática).recursive uses direct recursion (similar to the mathematical definition).recursive_with_accumulator usa recursión con acumulador (tail-call style).recursive_with_accumulator uses accumulator-based recursion (tail-call style).iterative usa bucles (sin recursión), logrando memoria constante O(1).iterative uses loops (no recursion), achieving constant O(1) memory.src/) de las pruebas (test/).src/) from tests (test/)._rec)container recursive
.- sum_of_first_n_rec(n)
if n == 0
return 0
return n + sum_of_first_n_rec(n - 1)
.- factorial_rec(n)
if n == 0
return 1
return n * factorial_rec(n - 1)
.- fibonacci_rec(n)
if n <= 1
return n
return fibonacci_rec(n - 1) + fibonacci_rec(n - 2)
.- greatest_common_divisor_rec(a, b)
if b == 0
return a
return greatest_common_divisor_rec(b, a % b)
.- least_common_multiple_rec(a, b)
return (a * b) / greatest_common_divisor_rec(a, b)
end container
_acc)container recursive_with_accumulator
.- sum_of_first_n_acc(n)
return sum_of_first_n_acc_help(n, 0)
.- sum_of_first_n_acc_help(n, acc)
if n <= 0
return acc
return sum_of_first_n_acc_help(n - 1, n + acc)
.- factorial_acc(n)
return factorial_acc_help(n, 1)
.- factorial_acc_help(n, acc)
if n <= 1
return acc
return factorial_acc_help(n - 1, n * acc)
.- fibonacci_acc(n)
return fibonacci_acc_help(n, 0, 1)
.- fibonacci_acc_help(n, acc2, acc1)
if n <= 0
return acc2
if n <= 2
return acc1 + acc2
return fibonacci_acc_help(n - 1, acc1, acc1 + acc2)
.- greatest_common_divisor_acc(a, b)
return greatest_common_divisor_acc_help(a, b)
.- greatest_common_divisor_acc_help(a, b)
if b == 0
return a
return greatest_common_divisor_acc_help(b, a % b)
.- least_common_multiple_acc(a, b)
return (a * b) / greatest_common_divisor_acc(a, b)
end container
_ite)container iterative
.- sum_of_first_n_ite(n)
result = 0
for i = 1 to n
result = result + i
return result
.- factorial_ite(n)
result = 1
for i = 2 to n
result = result * i
return result
.- fibonacci_ite(n)
if n <= 1
return n
acc2 = 0
acc1 = 1
for i = 2 to n
temp = acc1 + acc2
acc2 = acc1
acc1 = temp
return acc1
.- greatest_common_divisor_ite(a, b)
temp = 0
while b != 0
temp = b
b = a % b
a = temp
return a
.- least_common_multiple_ite(a, b)
return (a * b) / greatest_common_divisor_ite(a, b)
end container
Las pruebas unitarias deben verificar los siguientes casos para cada módulo y algoritmo:
| Módulo | Algoritmo | Entrada | Salida esperada |
|---|---|---|---|
recursive |
sum_of_first_n_rec(n) |
0 | 0 |
recursive |
sum_of_first_n_rec(n) |
3 | 6 |
recursive |
factorial_rec(n) |
0 | 1 |
recursive |
factorial_rec(n) |
4 | 24 |
recursive |
fibonacci_rec(n) |
0 | 0 |
recursive |
fibonacci_rec(n) |
1 | 1 |
recursive |
fibonacci_rec(n) |
6 | 8 |
recursive |
greatest_common_divisor_rec(a, b) |
12, 8 | 4 |
recursive |
greatest_common_divisor_rec(a, b) |
7, 5 | 1 |
recursive |
least_common_multiple_rec(a, b) |
4, 6 | 12 |
recursive |
least_common_multiple_rec(a, b) |
6, 8 | 24 |
recursive_with_accumulator |
sum_of_first_n_acc(n) |
0 | 0 |
recursive_with_accumulator |
sum_of_first_n_acc(n) |
3 | 6 |
recursive_with_accumulator |
factorial_acc(n) |
0 | 1 |
recursive_with_accumulator |
factorial_acc(n) |
4 | 24 |
recursive_with_accumulator |
fibonacci_acc(n) |
0 | 0 |
recursive_with_accumulator |
fibonacci_acc(n) |
1 | 1 |
recursive_with_accumulator |
fibonacci_acc(n) |
6 | 8 |
recursive_with_accumulator |
greatest_common_divisor_acc(a, b) |
12, 8 | 4 |
recursive_with_accumulator |
greatest_common_divisor_acc(a, b) |
7, 5 | 1 |
recursive_with_accumulator |
least_common_multiple_acc(a, b) |
4, 6 | 12 |
recursive_with_accumulator |
least_common_multiple_acc(a, b) |
6, 8 | 24 |
iterative |
sum_of_first_n_ite(n) |
0 | 0 |
iterative |
sum_of_first_n_ite(n) |
3 | 6 |
iterative |
factorial_ite(n) |
0 | 1 |
iterative |
factorial_ite(n) |
4 | 24 |
iterative |
fibonacci_ite(n) |
0 | 0 |
iterative |
fibonacci_ite(n) |
1 | 1 |
iterative |
fibonacci_ite(n) |
6 | 8 |
iterative |
greatest_common_divisor_ite(a, b) |
12, 8 | 4 |
iterative |
greatest_common_divisor_ite(a, b) |
7, 5 | 1 |
iterative |
least_common_multiple_ite(a, b) |
4, 6 | 12 |
iterative |
least_common_multiple_ite(a, b) |
6, 8 | 24 |
ES: A partir de este módulo en adelante, no se incluyen ejemplos de código en lenguajes específicos ni suites de prueba en pseudocódigo. Las especificaciones se definen mediante pseudocódigo de implementación, y cada implementador debe traducirlo al lenguaje de su elección siguiendo las convenciones y el framework/biblioteca de pruebas que corresponda (por ejemplo,
unittesten Python,JUniten Java,testingen Go, etc.).
EN: From this module onward, no specific language code examples or pseudocode test suites are included. Implementation specifications are defined through pseudocode, and each implementer must translate it to their language of choice following the conventions and the corresponding test framework/library (e.g.,unittestin Python,JUnitin Java,testingin Go, etc.).
programming_languages/
└── {language}/
└── core/
└── foundations/
└── numbers/
├── src/
│ └── numbers.ext # Único archivo: 3 enfoques en 1
└── test/
├── recursive_tests.ext # Tests: enfoque recursivo
├── recursive_with_acc_tests.ext # Tests: enfoque con acumulador
├── iterative_tests.ext # Tests: enfoque iterativo
└── run_tests.ext # Punto de entrada
ES: Los nombres de las funciones siguen el patrón
algoritmo_(rec|acc|ite)con helpers privadosalgoritmo_(rec|acc|ite)_help. Las suites de prueba están separadas por enfoque pero todas referencian el mismonumbers.ext. Para ejecutar las pruebas, usa el comando propio del framework/biblioteca de tu lenguaje. EN: Function names follow the patternalgorithm_(rec|acc|ite)with private helpersalgorithm_(rec|acc|ite)_help. Test suites are separated by approach but all reference the samenumbers.ext. To run the tests, use the command provided by your language’s test framework/library.
👉 El siguiente paso depende de tu ruta de aprendizaje — Continúa con los siguientes módulos de Core o explora otras secciones.
👉 The next step depends on your learning path — Continue with the next Core modules or explore other sections.
| *← Volver a 03_Unit_Test_Calculator | ↑ Inicio* |