phpOMS/Math/Number/Numbers.php
2017-02-03 20:37:08 +01:00

119 lines
2.3 KiB
PHP

<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace phpOMS\Math\Number;
/**
* Numbers class.
*
* @category Framework
* @package Utils
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Numbers
{
/**
* Is perfect number?
*
* @param int $n Number to test
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
public static function isPerfect(int $n) : bool
{
$sum = 0;
for ($i = 1; $i < $n; $i++) {
if ($n % $i == 0) {
$sum += $i;
}
}
return $sum == $n;
}
/**
* Is self describing number?
*
* @param int $n Number to test
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
public static function isSelfdescribing(int $n) : bool
{
$n = (string) $n;
$split = str_split($n);
foreach ($split as $place => $value) {
if (substr_count($n, (string) $place) != $value) {
return false;
}
}
return true;
}
/**
* Is square number?
*
* @param int $n Number to test
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
public static function isSquare(int $n) : bool
{
return abs(((int) sqrt($n)) * ((int) sqrt($n)) - $n) < 0.001;
}
/**
* Count trailling zeros
*
* @param int $n Number to test
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
public static function countTrailingZeros(int $n) : int
{
$count = 0;
while ($n !== 0) {
if ($n & 1 == 1) {
break;
} else {
$count++;
$n = $n >> 1;
}
}
return $count;
}
}