Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
Numbers | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
12 | |
100.00% |
1 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
isPerfect | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
isSelfdescribing | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
isSquare | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
countTrailingZeros | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Math\Number |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Math\Number; |
16 | |
17 | /** |
18 | * Numbers class. |
19 | * |
20 | * @package phpOMS\Math\Number |
21 | * @license OMS License 2.0 |
22 | * @link https://jingga.app |
23 | * @since 1.0.0 |
24 | */ |
25 | final class Numbers |
26 | { |
27 | public const SFLOAT = 1.175494351E-38; |
28 | |
29 | public const EPSILON = 4.88e-04; |
30 | |
31 | /** |
32 | * Constructor. |
33 | * |
34 | * @since 1.0.0 |
35 | * @codeCoverageIgnore |
36 | */ |
37 | private function __construct() |
38 | { |
39 | } |
40 | |
41 | /** |
42 | * Is perfect number? |
43 | * |
44 | * @param int $n Number to test |
45 | * |
46 | * @return bool |
47 | * |
48 | * @since 1.0.0 |
49 | */ |
50 | public static function isPerfect(int $n) : bool |
51 | { |
52 | $sum = 0; |
53 | |
54 | for ($i = 1; $i < $n; ++$i) { |
55 | if ($n % $i == 0) { |
56 | $sum += $i; |
57 | } |
58 | } |
59 | |
60 | return $sum === $n; |
61 | } |
62 | |
63 | /** |
64 | * Is self describing number? |
65 | * |
66 | * @param int $n Number to test |
67 | * |
68 | * @return bool |
69 | * |
70 | * @since 1.0.0 |
71 | */ |
72 | public static function isSelfdescribing(int $n) : bool |
73 | { |
74 | $n = (string) $n; |
75 | $split = \str_split($n); |
76 | |
77 | if ($split === false) { |
78 | return false; // @codeCoverageIgnore |
79 | } |
80 | |
81 | foreach ($split as $place => $value) { |
82 | if (\substr_count($n, (string) $place) != $value) { |
83 | return false; |
84 | } |
85 | } |
86 | |
87 | return true; |
88 | } |
89 | |
90 | /** |
91 | * Is square number? |
92 | * |
93 | * @param int $n Number to test |
94 | * |
95 | * @return bool |
96 | * |
97 | * @since 1.0.0 |
98 | */ |
99 | public static function isSquare(int $n) : bool |
100 | { |
101 | return \abs(((int) \sqrt($n)) * ((int) \sqrt($n)) - $n) < self::EPSILON; |
102 | } |
103 | |
104 | /** |
105 | * Count trailling zeros |
106 | * |
107 | * @param int $n Number to test |
108 | * |
109 | * @return int |
110 | * |
111 | * @since 1.0.0 |
112 | */ |
113 | public static function countTrailingZeros(int $n) : int |
114 | { |
115 | $count = 0; |
116 | while ($n !== 0) { |
117 | if (($n & 1) === 1) { |
118 | break; |
119 | } |
120 | |
121 | ++$count; |
122 | $n >>= 1; |
123 | } |
124 | |
125 | return $count; |
126 | } |
127 | } |