From bb82ea6f74b3a80339cf28205e09666e3811634f Mon Sep 17 00:00:00 2001 From: Armando Ceballos Date: Sat, 13 May 2023 17:15:18 -0600 Subject: [PATCH] Refactoriazacion de factorial --- README.md | 40 ++------------------ app/Commands/Factorial.php | 75 ++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index a8629ae..811353b 100644 --- a/README.md +++ b/README.md @@ -1,40 +1,8 @@ - +# Ejercicios ------- +## Factorial -

- -

+Este ejercicio se hace con el fin de entender recursividad al final se agrego tambien un ejemplo de tablas de multiplicar con recursividad -

- Build Status - Total Downloads - Latest Stable Version - License -

+## Triangulo de pascal -

This is a community project and not an official Laravel one

- -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. diff --git a/app/Commands/Factorial.php b/app/Commands/Factorial.php index d389feb..2d71780 100644 --- a/app/Commands/Factorial.php +++ b/app/Commands/Factorial.php @@ -12,7 +12,7 @@ class Factorial extends Command * * @var string */ - protected $signature = 'factorial'; + protected $signature = 'factorial {numero} {--grafica}'; /** * 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'; + 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. * @@ -28,13 +84,16 @@ class Factorial extends Command */ 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); }