math-php
Static Public Member Functions | List of all members
MathPHP\NumberTheory\Integer Class Reference

Static Public Member Functions

static isPerfectNumber (int $n)
 Detect if an integer is a perfect number. More...
 
static isPerfectPower (int $n)
 Detect if an integer is a perfect power. More...
 
static perfectPower (int $n)
 If n is a perfect power, compute an m and k such that mᵏ = n. More...
 
static primeFactorization (int $n)
 Prime factorization The prime factors of an integer. More...
 
static coprime (int $a, int $b)
 Coprime (relatively prime, mututally prime) Two integers a and b are said to be coprime if the only positive integer that divides both of them is 1. More...
 
static isOdd (int $x)
 Odd number. More...
 
static isEven (int $x)
 Even number. More...
 

Member Function Documentation

◆ coprime()

static MathPHP\NumberTheory\Integer::coprime ( int  $a,
int  $b 
)
static

Coprime (relatively prime, mututally prime) Two integers a and b are said to be coprime if the only positive integer that divides both of them is 1.

That is, the only common positive factor of the two numbers is 1. This is equivalent to their greatest common divisor being 1. https://en.wikipedia.org/wiki/Coprime_integers

Parameters
int$a
int$b
Returns
bool true if a and b are coprime; false otherwise

◆ isEven()

static MathPHP\NumberTheory\Integer::isEven ( int  $x)
static

Even number.

Parameters
int$x
Returns
bool true if x is even; false otherwise

◆ isOdd()

static MathPHP\NumberTheory\Integer::isOdd ( int  $x)
static

Odd number.

Parameters
int$x
Returns
bool true if x is odd; false otherwise

◆ isPerfectNumber()

static MathPHP\NumberTheory\Integer::isPerfectNumber ( int  $n)
static

Detect if an integer is a perfect number.

A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself

See also
https://en.wikipedia.org/wiki/Perfect_number
Parameters
int$n
Returns
bool

◆ isPerfectPower()

static MathPHP\NumberTheory\Integer::isPerfectPower ( int  $n)
static

Detect if an integer is a perfect power.

A perfect power is a positive integer that can be expressed as an integer power of another positive integer. If n is a perfect power, then exists m > 1 and k > 1 such that mᵏ = n. https://en.wikipedia.org/wiki/Perfect_power

Algorithm: For each divisor of n (as m), consider all possible values of k from 2 to log₂n.

  • If mᵏ = n, return true
  • If exhaust all possible mᵏ combinations, return false.
Parameters
int$n
Returns
bool True if n is a perfect power; false otherwise.

◆ perfectPower()

static MathPHP\NumberTheory\Integer::perfectPower ( int  $n)
static

If n is a perfect power, compute an m and k such that mᵏ = n.

A perfect power is a positive integer that can be expressed as an integer power of another positive integer. If n is a perfect power, then exists m > 1 and k > 1 such that mᵏ = n. https://en.wikipedia.org/wiki/Perfect_power

Algorithm: For each divisor of n (as m), consider all possible values of k from 2 to log₂n.

  • If mᵏ = n, return m and k
  • If exhaust all possible mᵏ combinations, return empty array.

An integer n could have multiple perfect power scenarios. Only one is returned.

Parameters
int$n
Returns
array [m, k]

◆ primeFactorization()

static MathPHP\NumberTheory\Integer::primeFactorization ( int  $n)
static

Prime factorization The prime factors of an integer.

https://en.wikipedia.org/wiki/Prime_factor

Todo:
Use a better algorithm.
Parameters
int$n
Returns
array of prime factors
Exceptions
Exception

The documentation for this class was generated from the following file: