-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.php
More file actions
33 lines (30 loc) · 745 Bytes
/
Copy pathFactorial.php
File metadata and controls
33 lines (30 loc) · 745 Bytes
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
<?php
namespace App;
class Factorial
{
public function factorial(int $number): ?int
{
if ($number < 1) {
return false;
}
if ($number === 1) {
return $number;
} else {
return $number * $this->factorial($number - 1);
}
}
public function factorialIter(int $number): ?int
{
$factorialIter = function ($acc, $number) use (&$factorialIter) {
if ($number < 1) {
return false;
}
if ($number === 1) {
return $acc;
} else {
return $factorialIter($acc * $number, $number - 1);
}
};
return $factorialIter(1, $number);
}
}