Se colocaran ejercios para practicar de codigobit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.7 KiB

  1. <?php
  2. namespace App\Commands;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use LaravelZero\Framework\Commands\Command;
  5. class Factorial extends Command
  6. {
  7. /**
  8. * The signature of the command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'factorial {numero} {--grafica}';
  13. /**
  14. * The description of the command.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'este comando es para calcular el factorial de un numero';
  19. private function factorial(int $numero, bool $operaciones = false): int
  20. {
  21. $factorial=1;
  22. $cadena = "";
  23. for ($i = $numero; $i > 0; $i--)
  24. {
  25. $cadena .= $factorial. ' * '.$i;
  26. $factorial = $factorial * $i;
  27. $cadena .= ' = '.$factorial."\n";
  28. }
  29. if ($operaciones)
  30. $this->line($cadena);
  31. return $factorial;
  32. }
  33. /*
  34. private static long factorial(int n1) {
  35. long miNum = 1;
  36. for(int i=1;i<=n1;i++) {
  37. miNum*=i;
  38. }
  39. return miNum;
  40. }
  41. def factorial(num):
  42. if num > 0:
  43. # Doing the factorial using recursion
  44. return int(num*factorial(num-1))
  45. else:
  46. return 1
  47. public static void multiplicar(int iTabla, int iNumero){
  48. if (iNumero>1)
  49. multiplicar(iTabla,iNumero-1);
  50. System.out.println(iTabla + "x" + iNumero + "=" + iTabla*iNumero);
  51. }
  52. */
  53. private function tablaMultiplicar($tabla, $numero){
  54. if ($numero > 1)
  55. $this->tablaMultiplicar($tabla, $numero-1);
  56. $this->line("{$tabla} X {$numero} = ".$tabla*$numero);
  57. }
  58. private function factorialRecursivo(int $numero, bool $operaciones = false): int
  59. {
  60. if ($numero > 0) {
  61. return $numero*$this->factorialRecursivo($numero-1);
  62. }
  63. else
  64. return 1;
  65. }
  66. /**
  67. * Execute the console command.
  68. *
  69. * @return mixed
  70. */
  71. public function handle()
  72. {
  73. $this->title('Calculo de factorial');
  74. $this->info('
  75. El factorial de un entero positivo n, el factorial de n o n
  76. factorial se define en principio como el producto de todos los números
  77. enteros positivos desde 1 hasta n.
  78. ');
  79. $factorial = $this->factorialRecursivo($this->argument('numero'), $this->option('grafica'));
  80. $this->line("El numero factorial es el siguiente = {$factorial}");
  81. $this->line("Su tabla de multiplicar es:");
  82. $this->tablaMultiplicar($this->argument('numero'), 10);
  83. }
  84. /**
  85. * Define the command's schedule.
  86. *
  87. * @param \Illuminate\Console\Scheduling\Schedule $schedule
  88. * @return void
  89. */
  90. public function schedule(Schedule $schedule): void
  91. {
  92. // $schedule->command(static::class)->everyMinute();
  93. }
  94. }