mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-20 21:38:41 +00:00
Numbers extension
This commit is contained in:
parent
772a001f73
commit
4b4f8b1dbc
|
|
@ -1,9 +1,87 @@
|
|||
class Integer implements Number {
|
||||
public function isPrime() {
|
||||
<?php
|
||||
class Integer implements Number
|
||||
{
|
||||
public static function isInteger($value) : bool
|
||||
{
|
||||
return is_int($value);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Greatest common diviser.
|
||||
*
|
||||
* @param int $n Number one
|
||||
* @param int $m Number two
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn
|
||||
*/
|
||||
public static function greatestCommonDivisor(int $n, int $m) : int
|
||||
{
|
||||
while (true) {
|
||||
if ($n === $m) {
|
||||
return $m;
|
||||
} if ($n > $m) {
|
||||
$n -= $m;
|
||||
} else {
|
||||
$m -= $n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getIntegerFactorization() {
|
||||
public static function trialFactorization(int $value)
|
||||
{
|
||||
if ($value < 2) {
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
$factors = [];
|
||||
$primes = Prime::sieveOfEratosthenes((int) $value**0.5);
|
||||
|
||||
for($primes as $prime) {
|
||||
if($prime*$prime > $value) {
|
||||
break;
|
||||
}
|
||||
|
||||
while($value%$prime === 0) {
|
||||
$factors[] = $prime;
|
||||
$value /= $prime;
|
||||
}
|
||||
}
|
||||
|
||||
if($value > 1) {
|
||||
$factors[] = $value;
|
||||
}
|
||||
|
||||
return $factors;
|
||||
}
|
||||
|
||||
public static function pollardsRho($value, $x = 2, $factor = 1, $cycleSize = 2, $xFixed = 2)
|
||||
{
|
||||
while($factor === 1) {
|
||||
for($i = 1; $i < $cycleSize && $factor <= 1; $i++) {
|
||||
$x = ($x*$x+1)%$value;
|
||||
$factor = self::greatestCommonDivisor($x-$xFixed, $value);
|
||||
}
|
||||
|
||||
$cycleSize *= 2;
|
||||
$xFixed = $x;
|
||||
}
|
||||
|
||||
return $factor;
|
||||
}
|
||||
|
||||
public static function fermatFactor(int $value)
|
||||
{
|
||||
$a = $value;
|
||||
$b2 = $a*$a - $value;
|
||||
|
||||
while(abs((int) round(sqrt($b2), 0) - sqrt($b2)) > 0.0001) {
|
||||
$a += 1;
|
||||
$b2 = $a*$a - $value;
|
||||
}
|
||||
|
||||
return $a - sqrt($b2);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1,9 @@
|
|||
class Natural implements Number {
|
||||
<?php
|
||||
|
||||
class Natural implements Number
|
||||
{
|
||||
public static function isNatural($value) : bool
|
||||
{
|
||||
return is_int($value) && $value >= 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -142,28 +142,4 @@ class Numbers
|
|||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Greatest common diviser.
|
||||
*
|
||||
* @param int $n Number one
|
||||
* @param int $m Number two
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn
|
||||
*/
|
||||
public static function greatestCommonDivisor(int $n, int $m) : int
|
||||
{
|
||||
while (true) {
|
||||
if ($n === $m) {
|
||||
return $m;
|
||||
} if ($n > $m) {
|
||||
$n -= $m;
|
||||
} else {
|
||||
$m -= $n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -116,4 +116,45 @@ class Prime
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function sieveOfEratosthenes(int $n) : array
|
||||
{
|
||||
$number = 2;
|
||||
$range = range(2, $n);
|
||||
$primes = array_combine($range, $range);
|
||||
|
||||
while ($number*$number < $n) {
|
||||
for ($i = $number; $i <= $n; $i += $number) {
|
||||
if ($i == $number) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($primes[$i]);
|
||||
}
|
||||
|
||||
$number = next($primes);
|
||||
}
|
||||
|
||||
return $primes;
|
||||
}
|
||||
|
||||
public function isPrime(int $n) : bool
|
||||
{
|
||||
$i = 2;
|
||||
|
||||
if ($n === 2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$sqrtN = sqrt($n);
|
||||
while ($i <= $sqrtN) {
|
||||
if ($n % $i === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user