30秒的PHP代码片段(2)数学 - Math

栏目: PHP · 发布时间: 6年前

内容简介:精选的有用PHP片段集合,您可以在30秒或更短的时间内理解这些片段。

本文来自GitHub开源项目

点我跳转

30秒的 PHP 代码片段

30秒的PHP代码片段(2)数学 - Math

精选的有用PHP片段集合,您可以在30秒或更短的时间内理解这些片段。

数学函数

average

返回两个或多个数字的平均值。

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

Examples

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

factorial(阶乘)

计算一个数的阶乘。

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

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

Examples

factorial(6); // 720

fibonacci(斐波那契数列)

生成包含斐波那契数列的数组,直到第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(最大公约数)

计算两个或多个数之间的最大公约数。

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

如果给定的数字是偶数,则返回 true ,否则返回 false

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

Examples

isEven(4); // true

isPrime

检查提供的整数是否是素数。

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

返回两个或多个数字的最小公倍数。

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

返回数字数组的中间值。

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

从提供的数组中返回最大的数的个数。

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

从提供的数组中返回最小的数的个数。

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(约等于)

检查两个数字是否近似相等。使用abs()将两个值的绝对值与进行比较。省略第三个参数,以便使用默认值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

num 放在边界值 ab 指定的包含范围内。如果 num 在该范围内,则返回 num 。否则,返回该范围内最近的数字。

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First HTML5 Programming

Head First HTML5 Programming

Eric Freeman、Elisabeth Robson / O'Reilly Media / 2011-10-18 / USD 49.99

What can HTML5 do for you? If you're a web developer looking to use this new version of HTML, you might be wondering how much has really changed. Head First HTML5 Programming introduces the key featur......一起来看看 《Head First HTML5 Programming》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具