-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInteger.php
More file actions
64 lines (51 loc) · 1.13 KB
/
Copy pathInteger.php
File metadata and controls
64 lines (51 loc) · 1.13 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* This file is part of the core-library package.
*
* (c) 2023 WEBEWEB
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace WBW\Library\Common\Math;
/**
* Integer.
*
* @author webeweb <https://github.com/webeweb>
* @package WBW\Library\Common\Math
*/
class Integer {
/**
* Factorial.
*
* @param int|null $n The number.
* @return float|null Returns the factorial.
*/
public static function factorial(?int $n): ?float {
if (null === $n || $n < 0) {
return null;
}
if (0 === $n) {
return 1;
}
$result = $n;
while (1 < --$n) {
$result *= $n;
}
return (float) $result;
}
/**
* Summation.
*
* @param int|null $n The number.
* @return float|null Returns the summation.
*/
public static function summation(?int $n): ?float {
if (null === $n) {
return null;
}
$q = $n * ($n + 1);
return $q / 2;
}
}