| @@ -1,40 +1,8 @@ | |||||
| <a href="https://supportukrainenow.org/"><img src="https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct.svg" width="100%"></a> | |||||
| # Ejercicios | |||||
| ## Factorial | |||||
| <p align="center"> | |||||
| <img title="Laravel Zero" height="100" src="https://raw.githubusercontent.com/laravel-zero/docs/master/images/logo/laravel-zero-readme.png" /> | |||||
| </p> | |||||
| Este ejercicio se hace con el fin de entender recursividad al final se agrego tambien un ejemplo de tablas de multiplicar con recursividad | |||||
| <p align="center"> | |||||
| <a href="https://github.com/laravel-zero/framework/actions"><img src="https://github.com/laravel-zero/laravel-zero/actions/workflows/tests.yml/badge.svg" alt="Build Status"></img></a> | |||||
| <a href="https://packagist.org/packages/laravel-zero/framework"><img src="https://img.shields.io/packagist/dt/laravel-zero/framework.svg" alt="Total Downloads"></a> | |||||
| <a href="https://packagist.org/packages/laravel-zero/framework"><img src="https://img.shields.io/packagist/v/laravel-zero/framework.svg?label=stable" alt="Latest Stable Version"></a> | |||||
| <a href="https://packagist.org/packages/laravel-zero/framework"><img src="https://img.shields.io/packagist/l/laravel-zero/framework.svg" alt="License"></a> | |||||
| </p> | |||||
| ## Triangulo de pascal | |||||
| <h4> <center>This is a <bold>community project</bold> and not an official Laravel one </center></h4> | |||||
| Laravel Zero was created by [Nuno Maduro](https://github.com/nunomaduro) and [Owen Voke](https://github.com/owenvoke), and is a micro-framework that provides an elegant starting point for your console application. It is an **unofficial** and customized version of Laravel optimized for building command-line applications. | |||||
| - Built on top of the [Laravel](https://laravel.com) components. | |||||
| - Optional installation of Laravel [Eloquent](https://laravel-zero.com/docs/database/), Laravel [Logging](https://laravel-zero.com/docs/logging/) and many others. | |||||
| - Supports interactive [menus](https://laravel-zero.com/docs/build-interactive-menus/) and [desktop notifications](https://laravel-zero.com/docs/send-desktop-notifications/) on Linux, Windows & MacOS. | |||||
| - Ships with a [Scheduler](https://laravel-zero.com/docs/task-scheduling/) and a [Standalone Compiler](https://laravel-zero.com/docs/build-a-standalone-application/). | |||||
| - Integration with [Collision](https://github.com/nunomaduro/collision) - Beautiful error reporting | |||||
| ## Documentation | |||||
| For full documentation, visit [laravel-zero.com](https://laravel-zero.com/). | |||||
| ## Support the development | |||||
| **Do you like this project? Support it by donating** | |||||
| - PayPal: [Donate](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L) | |||||
| - Patreon: [Donate](https://www.patreon.com/nunomaduro) | |||||
| ## License | |||||
| Laravel Zero is an open-source software licensed under the MIT license. | |||||
| @@ -12,7 +12,7 @@ class Factorial extends Command | |||||
| * | * | ||||
| * @var string | * @var string | ||||
| */ | */ | ||||
| protected $signature = 'factorial'; | |||||
| protected $signature = 'factorial {numero} {--grafica}'; | |||||
| /** | /** | ||||
| * The description of the command. | * The description of the command. | ||||
| @@ -21,6 +21,62 @@ class Factorial extends Command | |||||
| */ | */ | ||||
| protected $description = 'este comando es para calcular el factorial de un numero'; | protected $description = 'este comando es para calcular el factorial de un numero'; | ||||
| private function factorial(int $numero, bool $operaciones = false): int | |||||
| { | |||||
| $factorial=1; | |||||
| $cadena = ""; | |||||
| for ($i = $numero; $i > 0; $i--) | |||||
| { | |||||
| $cadena .= $factorial. ' * '.$i; | |||||
| $factorial = $factorial * $i; | |||||
| $cadena .= ' = '.$factorial."\n"; | |||||
| } | |||||
| if ($operaciones) | |||||
| $this->line($cadena); | |||||
| return $factorial; | |||||
| } | |||||
| /* | |||||
| private static long factorial(int n1) { | |||||
| long miNum = 1; | |||||
| for(int i=1;i<=n1;i++) { | |||||
| miNum*=i; | |||||
| } | |||||
| return miNum; | |||||
| } | |||||
| def factorial(num): | |||||
| if num > 0: | |||||
| # Doing the factorial using recursion | |||||
| return int(num*factorial(num-1)) | |||||
| else: | |||||
| return 1 | |||||
| public static void multiplicar(int iTabla, int iNumero){ | |||||
| if (iNumero>1) | |||||
| multiplicar(iTabla,iNumero-1); | |||||
| System.out.println(iTabla + "x" + iNumero + "=" + iTabla*iNumero); | |||||
| } | |||||
| */ | |||||
| private function tablaMultiplicar($tabla, $numero){ | |||||
| if ($numero > 1) | |||||
| $this->tablaMultiplicar($tabla, $numero-1); | |||||
| $this->line("{$tabla} X {$numero} = ".$tabla*$numero); | |||||
| } | |||||
| private function factorialRecursivo(int $numero, bool $operaciones = false): int | |||||
| { | |||||
| if ($numero > 0) { | |||||
| return $numero*$this->factorialRecursivo($numero-1); | |||||
| } | |||||
| else | |||||
| return 1; | |||||
| } | |||||
| /** | /** | ||||
| * Execute the console command. | * Execute the console command. | ||||
| * | * | ||||
| @@ -28,13 +84,16 @@ class Factorial extends Command | |||||
| */ | */ | ||||
| public function handle() | public function handle() | ||||
| { | { | ||||
| $num=10; | |||||
| $fact=1; | |||||
| for ($i = $num; $i > 0; $i--) | |||||
| { | |||||
| $fact= $fact * $i; | |||||
| } | |||||
| echo "El numero factorial es el siguiente = ".$fact; | |||||
| $this->title('Calculo de factorial'); | |||||
| $this->info(' | |||||
| El factorial de un entero positivo n, el factorial de n o n | |||||
| factorial se define en principio como el producto de todos los números | |||||
| enteros positivos desde 1 hasta n. | |||||
| '); | |||||
| $factorial = $this->factorialRecursivo($this->argument('numero'), $this->option('grafica')); | |||||
| $this->line("El numero factorial es el siguiente = {$factorial}"); | |||||
| $this->line("Su tabla de multiplicar es:"); | |||||
| $this->tablaMultiplicar($this->argument('numero'), 10); | |||||
| } | } | ||||