|
| 1 | +# Challenge Description and Solution |
| 2 | + |
| 3 | +## English Version |
| 4 | + |
| 5 | +### Challenge Description |
| 6 | +Develop a recursive or iterative function to calculate the factorial of a non-negative integer. Consider handling special cases. |
| 7 | + |
| 8 | +### Code Explanation |
| 9 | +The code presents two functions to calculate the factorial: |
| 10 | + |
| 11 | +- `factorial_recursive(n)`: Calculates the factorial recursively. If `n` is negative, it raises an error. If `n` is 0 or 1, it returns 1. Otherwise, it returns `n` multiplied by the factorial of `n-1`. |
| 12 | + |
| 13 | +- `factorial_iterative(n)`: Calculates the factorial iteratively. If `n` is negative, it raises an error. It initializes `result` to 1 and successively multiplies by each number from 2 to `n`. |
| 14 | + |
| 15 | +Relevant code: |
| 16 | + |
| 17 | +```python |
| 18 | +def factorial_recursive(n): |
| 19 | + if n < 0: |
| 20 | + raise ValueError("Factorial is not defined for negative numbers") |
| 21 | + if n == 0 or n == 1: |
| 22 | + return 1 |
| 23 | + return n * factorial_recursive(n - 1) |
| 24 | + |
| 25 | +def factorial_iterative(n): |
| 26 | + if n < 0: |
| 27 | + raise ValueError("Factorial is not defined for negative numbers") |
| 28 | + result = 1 |
| 29 | + for i in range(2, n + 1): |
| 30 | + result *= i |
| 31 | + return result |
| 32 | +``` |
| 33 | + |
| 34 | +The `if __name__ == "__main__":` block contains an example usage that calculates the factorial of several values using both methods. |
| 35 | + |
| 36 | +```python |
| 37 | +if __name__ == "__main__": |
| 38 | + test_values = [0, 1, 5, 7] |
| 39 | + for val in test_values: |
| 40 | + print(f"Factorial of {val} (recursive): {factorial_recursive(val)}") |
| 41 | + print(f"Factorial of {val} (iterative): {factorial_iterative(val)}") |
| 42 | +``` |
| 43 | + |
| 44 | +## Versión en Español |
| 45 | + |
| 46 | +### Descripción del Reto |
| 47 | +Desarrolla una función recursiva o iterativa para calcular el factorial de un número entero no negativo. Considera el manejo de casos especiales como números negativos. |
| 48 | + |
| 49 | +### Explicación del Código |
| 50 | +El código presenta dos funciones para calcular el factorial: |
| 51 | + |
| 52 | +- `factorial_recursive(n)`: Calcula el factorial de forma recursiva. Si `n` es negativo, lanza un error. Si `n` es 0 o 1, retorna 1. En otro caso, retorna `n` multiplicado por el factorial de `n-1`. |
| 53 | + |
| 54 | +- `factorial_iterative(n)`: Calcula el factorial de forma iterativa. Si `n` es negativo, lanza un error. Inicializa `result` en 1 y multiplica sucesivamente por cada número desde 2 hasta `n`. |
| 55 | + |
| 56 | +Código relevante: |
| 57 | + |
| 58 | +```python |
| 59 | +def factorial_recursive(n): |
| 60 | + if n < 0: |
| 61 | + raise ValueError("Factorial is not defined for negative numbers") |
| 62 | + if n == 0 or n == 1: |
| 63 | + return 1 |
| 64 | + return n * factorial_recursive(n - 1) |
| 65 | + |
| 66 | +def factorial_iterative(n): |
| 67 | + if n < 0: |
| 68 | + raise ValueError("Factorial is not defined for negative numbers") |
| 69 | + result = 1 |
| 70 | + for i in range(2, n + 1): |
| 71 | + result *= i |
| 72 | + return result |
| 73 | +``` |
| 74 | + |
| 75 | +El bloque `if __name__ == "__main__":` contiene un ejemplo de uso que calcula el factorial de varios valores usando ambos métodos. |
| 76 | + |
| 77 | +```python |
| 78 | +if __name__ == "__main__": |
| 79 | + test_values = [0, 1, 5, 7] |
| 80 | + for val in test_values: |
| 81 | + print(f"Factorial of {val} (recursive): {factorial_recursive(val)}") |
| 82 | + print(f"Factorial of {val} (iterative): {factorial_iterative(val)}") |
0 commit comments