diff --git a/Localization/Localization.php b/Localization/Localization.php index c4a289a13..fb8d2ba9e 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -39,7 +39,7 @@ class Localization * @var string * @since 1.0.0 */ - private $country = ISO3166TwoEnum::C_USA; + private $country = ISO3166TwoEnum::_USA; /** * Timezone. * @@ -60,7 +60,7 @@ class Localization * @var string * @since 1.0.0 */ - private $currency = ISO4217Enum::C_USD; + private $currency = ISO4217Enum::_USD; /** * Number format. * diff --git a/Math/Functions/Fibunacci.php b/Math/Functions/Fibunacci.php index 1ec0b103d..8cdd28cc7 100644 --- a/Math/Functions/Fibunacci.php +++ b/Math/Functions/Fibunacci.php @@ -14,7 +14,7 @@ * @link http://orange-management.com */ -namespace phpOMS\Math; +namespace phpOMS\Math\Functions; use phpOMS\Math\Number\Numbers; @@ -60,17 +60,15 @@ class Fibunacci */ public static function fibunacci(int $n, int $start = 1) : int { - if ($n < 2) { - return 0; - } elseif ($n < 4) { + if ($n < 3) { return $start; } - $old_1 = 0; + $old_1 = $start; $old_2 = $start; $fib = 0; - for ($i = 4; $i < $n; $i++) { + for ($i = 2; $i < $n; $i++) { $fib = $old_1 + $old_2; $old_1 = $old_2; $old_2 = $fib; diff --git a/Math/Geometry/ConvexHull/MonotoneChain.php b/Math/Geometry/ConvexHull/MonotoneChain.php index ccf75f508..b802ac0d7 100644 --- a/Math/Geometry/ConvexHull/MonotoneChain.php +++ b/Math/Geometry/ConvexHull/MonotoneChain.php @@ -13,7 +13,7 @@ * @version 1.0.0 * @link http://orange-management.com */ -namespace phpOMS\Math\Geometry; +namespace phpOMS\Math\Geometry\ConvexHull; /** * Andrew's monotone chain convex hull algorithm class. diff --git a/Math/Number/Integer.php b/Math/Number/Integer.php index e187eb475..9edce5227 100644 --- a/Math/Number/Integer.php +++ b/Math/Number/Integer.php @@ -125,10 +125,10 @@ class Integer */ public static function greatestCommonDivisor(int $n, int $m) : int { - while (true) { - if ($n === $m) { - return $m; - } + $n = abs($n); + $m = abs($m); + + while ($n !== $m) { if ($n > $m) { $n -= $m; } else { @@ -136,7 +136,7 @@ class Integer } } - return 1; + return $m; } /** @@ -154,17 +154,18 @@ class Integer */ public static function fermatFactor(int $value, int $limit = 1000000) : array { - if (($value % 2) !== 0) { + if (($value % 2) === 0) { throw new \Exception('Only odd integers are allowed'); } $a = (int) ceil(sqrt($value)); - $b2 = $a * $a - $value; + $b2 = (int) ($a * $a - $value); $i = 1; while (!Numbers::isSquare($b2) && $i < $limit) { + $i++; $a += 1; - $b2 = $a * $a - $value; + $b2 = (int) ($a * $a - $value); } return [(int) round($a - sqrt($b2)), (int) round($a + sqrt($b2))]; diff --git a/Math/Number/Numbers.php b/Math/Number/Numbers.php index 3d06f26b6..06a8a47d0 100644 --- a/Math/Number/Numbers.php +++ b/Math/Number/Numbers.php @@ -86,36 +86,7 @@ class Numbers */ public static function isSquare(int $n) : bool { - $goodMask = 0xC840C04048404040; // 0xC840C04048404040 computed below - - for ($i = 0; $i < 64; ++$i) { - $goodMask |= PHP_INT_MIN >> ($i * $i); - } - - // This tests if the 6 least significant bits are right. - // Moving the to be tested bit to the highest position saves us masking. - if ($goodMask << $n >= 0) { - return false; - } - - $numberOfTrailingZeros = self::countTrailingZeros($n); - // Each square ends with an even number of zeros. - if (($numberOfTrailingZeros & 1) !== 0) { - return false; - } - - $n >>= $numberOfTrailingZeros; - // Now x is either 0 or odd. - // In binary each odd square ends with 001. - // Postpone the sign test until now; handle zero in the branch. - if (($n & 7) != 1 | $n <= 0) { - return $n === 0; - } - // Do it in the classical way. - // The correctness is not trivial as the conversion from long to double is lossy! - $tst = (int) sqrt($n); - - return $tst * $tst === $n; + return abs(((int) sqrt($n)) * ((int) sqrt($n)) - $n) < 0.001; } /** diff --git a/Math/Number/Prime.php b/Math/Number/Prime.php index e791ad9d4..7076885d5 100644 --- a/Math/Number/Prime.php +++ b/Math/Number/Prime.php @@ -158,7 +158,7 @@ class Prime * @since 1.0.0 * @author Dennis Eichhorn */ - public function isPrime(int $n) : bool + public static function isPrime(int $n) : bool { $i = 2; diff --git a/Math/Shape/D2/D2ShapeInterface.php b/Math/Shape/D2/D2ShapeInterface.php index 93c8f2a75..e3a54ef29 100644 --- a/Math/Shape/D2/D2ShapeInterface.php +++ b/Math/Shape/D2/D2ShapeInterface.php @@ -30,5 +30,4 @@ use phpOMS\Math\Shape\ShapeInterface; */ interface D2ShapeInterface extends ShapeInterface { - public static function getPerimeter() : float; } diff --git a/Math/Shape/D3/Cone.php b/Math/Shape/D3/Cone.php index bd8ddb98a..52ec7800c 100644 --- a/Math/Shape/D3/Cone.php +++ b/Math/Shape/D3/Cone.php @@ -88,8 +88,8 @@ class Cone implements D3ShapeInterface * @since 1.0.0 * @author Dennis Eichhorn */ - public static function getHeight(float $V, float $r) : float + public static function getHeightFromVolume(float $V, float $r) : float { - return 4 * $V / (pi() * $r ** 2); + return 3 * $V / (pi() * $r ** 2); } } diff --git a/Math/Shape/D3/D3ShapeInterface.php b/Math/Shape/D3/D3ShapeInterface.php index 9686f68c8..fb2401a53 100644 --- a/Math/Shape/D3/D3ShapeInterface.php +++ b/Math/Shape/D3/D3ShapeInterface.php @@ -28,7 +28,6 @@ use phpOMS\Math\Shape\ShapeInterface; * @link http://orange-management.com * @since 1.0.0 */ -interface D2ShapeInterface extends ShapeInterface +interface D3ShapeInterface extends ShapeInterface { - public static function getVolume() : float; } diff --git a/Math/Shape/ShapeInterface.php b/Math/Shape/ShapeInterface.php index 3618dfec8..bc957edb1 100644 --- a/Math/Shape/ShapeInterface.php +++ b/Math/Shape/ShapeInterface.php @@ -28,5 +28,4 @@ namespace phpOMS\Math\Shape; */ interface ShapeInterface { - public static function getSurface() : float; } diff --git a/Utils/Compression/LZW.php b/Utils/Compression/LZW.php index f5672ded7..62804fee2 100644 --- a/Utils/Compression/LZW.php +++ b/Utils/Compression/LZW.php @@ -81,8 +81,8 @@ class LZW implements CompressionInterface $w = chr($compressed[0]); $result = $w; - $count = count($compressed); + for ($i = 1; $i < $count; $i++) { $k = $compressed[$i]; @@ -97,7 +97,7 @@ class LZW implements CompressionInterface } $result .= $entry; - $dictionary[$dictSize++] = $w + $entry[0]; + $dictionary[$dictSize++] = $w . $entry[0]; $w = $entry; }