30 second PHP snippet Math math

This article is from GitHub open source project

I jump.

30 second snippet of PHP code

A selection of useful PHP snippets that you can understand in 30 seconds or less.

Mathematical function

average

Returns the average of two or more numbers.

function average(...$items)
{
    $count = count($items);
    
    return $count === 0 ? 0 : array_sum($items) / $count;
}

Examples

average(1, 2, 3); // 2

Factorial

Calculate the factorial of a number.

function factorial($n)
{
    if ($n <= 1) {
        return 1;
    }

    return $n * factorial($n - 1);
}

Examples

factorial(6); // 720

Fibonacci (Fibonacci Series)

Generate an array containing Fibonacci sequences up to item n.

function fibonacci($n)
{
    $sequence = [0, 1];

    for ($i = 2; $i < $n; $i++) {
        $sequence[$i] = $sequence[$i-1] + $sequence[$i-2];
    }

    return $sequence;
}

Examples

fibonacci(6); // [0, 1, 1, 2, 3, 5]

GCD (maximum common divisor)

Calculates the maximum common divisor between two or more numbers.

function gcd(...$numbers)
{
    if (count($numbers) > 2) {
        return array_reduce($numbers, 'gcd');
    }

    $r = $numbers[0] % $numbers[1];
    return $r === 0 ? abs($numbers[1]) : gcd($numbers[1], $r);
}

Examples

gcd(8, 36); // 4
gcd(12, 8, 32); // 4

isEven

Returns true if the given number is even, otherwise false.

function isEven($number)
{
    return ($number % 2) === 0;
}

Examples

isEven(4); // true

isPrime

Checks if the supplied integer is a prime.

function isPrime($number)
{
    $boundary = floor(sqrt($number));
    for ($i = 2; $i <= $boundary; $i++) {
        if ($number % $i === 0) {
            return false;
        }
    }

    return $number >= 2;
}

Examples

isPrime(3); // true

lcm

Returns the least common multiple of two or more numbers.

function lcm(...$numbers)
{
    $ans = $numbers[0];
    for ($i = 1, $max = count($numbers); $i < $max; $i++) {
        $ans = (($numbers[$i] * $ans) / gcd($numbers[$i], $ans));
    }

    return $ans;
}

Examples

lcm(12, 7); // 84
lcm(1, 3, 4, 5); // 60

median

Returns the middle value of an array of numbers.

function median($numbers)
{
    sort($numbers);
    $totalNumbers = count($numbers);
    $mid = floor($totalNumbers / 2);

    return ($totalNumbers % 2) === 0 ? ($numbers[$mid - 1] + $numbers[$mid]) / 2 : $numbers[$mid];
}

Examples

median([1, 3, 3, 6, 7, 8, 9]); // 6
median([1, 2, 3, 6, 7, 9]); // 4.5

maxN

Returns the maximum number of items from the provided array.

function maxN($numbers)
{
    $maxValue = max($numbers);
    $maxValueArray = array_filter($numbers, function ($value) use ($maxValue) {
        return $maxValue === $value;
    });

    return count($maxValueArray);
}

Examples

maxN([1, 2, 3, 4, 5, 5]); // 2
maxN([1, 2, 3, 4, 5]); // 1

minN

Returns the minimum number of items from the provided array.

function minN($numbers)
{
    $minValue = min($numbers);
    $minValueArray = array_filter($numbers, function ($value) use ($minValue) {
        return $minValue === $value;
    });

    return count($minValueArray);
}

Examples

minN([1, 1, 2, 3, 4, 5, 5]); // 2
minN([1, 2, 3, 4, 5]); // 1

Approximatelyequal (approximately equal to)

Check that the two numbers are approximately equal. Use abs() to compare the absolute values of the two values with. Omit the third parameter to use the default value of 0.001.

function approximatelyEqual($number1, $number2, $epsilon = 0.001)
{
    return abs($number1 - $number2) < $epsilon;
}

Examples

approximatelyEqual(10.0, 10.00001); // true
approximatelyEqual(10.0, 10.01); // false

clampNumber

Place num in the inclusion range specified by boundary values a and b. If num is in the range, Num is returned. Otherwise, the latest number in the range is returned.

function clampNumber($num, $a, $b)
{
    return max(min($num, max($a, $b)), min($a, $b));
}

Examples

clampNumber(2, 3, 5); // 3
clampNumber(1, -1, -5); // -1

Related articles:

30 second PHP snippet (1) Array

Tags: PHP github less

Posted on Tue, 03 Dec 2019 13:14:31 -0500 by nicdp