Se colocaran ejercios para practicar de codigobit
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Factorial.php 2.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. }