aboutsummaryrefslogtreecommitdiff
path: root/challenge-153/lubos-kolouch/php/ch-2.php
blob: d7ab361c9f90b02a82f58c1f74897dc2d171e8a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

function calculate_factorial(int $what){

    $fact = 1;

    foreach (range(1, $what) as $i) {
        $fact *= $i;
    }

    return $fact;
}


function get_factorions_sum(int $what){

    $my_sum = 0;
    foreach (str_split($what) as $i) {
        $my_sum += calculate_factorial($i);
    }

    return $my_sum;
}


function is_equal(int $what){
    return $what == get_factorions_sum($what);
}


is_equal(145) == 1 or throw new Exception("Test 1 failed");
is_equal(123) == 0 or throw new Exception("Test 2 failed");

?>