Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.73 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. function algo($a) {
  5.     if (!filter_var($a, FILTER_VALIDATE_INT) || $a <= 0 ) {
  6.         Throw new Exception('Poza zakresem');
  7.     }
  8.     if ($a <= 2) {
  9.         return 2;
  10.     } else {
  11.         return algo($a-1) + (2 * algo($a-1));
  12.     }
  13. }
  14. function algo2($a) {
  15.     if (!filter_var($a, FILTER_VALIDATE_INT) || $a <= 0 ) {
  16.         Throw new Exception('Poza zakresem');
  17.     }
  18.     if ($a <= 2) {
  19.         return 2;
  20.     } else {
  21.         $x = 2;
  22.         for ($i = 0; $i < $a; $i++) {
  23.             if ($i <= 2) {
  24.                 $x = 2;
  25.             }
  26.             $x = $x + (2*$x);
  27.         }
  28.         return $x;
  29.     }
  30. }
  31. try
  32. {
  33.    echo algo(4)."\n";
  34.    echo algo2(4);
  35. }
  36. catch (Exception $e)
  37. {
  38.     echo $e->getMessage();
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement