Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Rekurencja w klasie
-greg712-
post
Post #1





Goście







Witam serdecznie,
Mam problem z ukończeniem pewnego ćwiczenia, opierającego się na wyliczeniu silni rekurencyjnie, poniżej kawałek kodu:

  1. private function SilniaR(){
  2. return $this->SilniaR($this->argument - 1) * $this->argument;
  3. }


Przy próbie otwarcia tego ćwiczenia, wyrzuca mi:
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 261904 bytes) in C:\xampp\htdocs\learning\zad1.php on line 18

Linia 18 to akurat ten return. Prośba o pomoc (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
untorched
post
Post #2





Grupa: Zarejestrowani
Postów: 318
Pomógł: 76
Dołączył: 27.12.2011
Skąd: Dąbrowa Górnicza

Ostrzeżenie: (0%)
-----


OMG.
  1. <?php
  2.  
  3. class Silnia {
  4.  
  5. private $argument = 5; // Jakaś tam prywatna wartość
  6.  
  7. public function SilniaR()
  8. {
  9. if($this->argument == 0)
  10. {
  11. return 1;
  12. }
  13. else
  14. {
  15. $liczba = $this->argument;
  16. $this->argument = $this->argument - 1;
  17. $licz = $liczba * $this->SilniaR();
  18. return $licz;
  19. }
  20. }
  21.  
  22. };
  23.  
  24. $silnia = new Silnia();
  25.  
  26. echo $silnia->SilniaR();
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 26.12.2025 - 21:34