Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function algo($a) {
- if (!filter_var($a, FILTER_VALIDATE_INT) || $a <= 0 ) {
- Throw new Exception('Poza zakresem');
- }
- if ($a <= 2) {
- return 2;
- } else {
- return algo($a-1) + (2 * algo($a-1));
- }
- }
- function algo2($a) {
- if (!filter_var($a, FILTER_VALIDATE_INT) || $a <= 0 ) {
- Throw new Exception('Poza zakresem');
- }
- if ($a <= 2) {
- return 2;
- } else {
- $x = 2;
- for ($i = 0; $i < $a; $i++) {
- if ($i <= 2) {
- $x = 2;
- }
- $x = $x + (2*$x);
- }
- return $x;
- }
- }
- try
- {
- echo algo(4)."\n";
- echo algo2(4);
- }
- catch (Exception $e)
- {
- echo $e->getMessage();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement