Home Backend Development PHP Tutorial php计算两个整数的最大公约数常用算法小结_php技巧

php计算两个整数的最大公约数常用算法小结_php技巧

May 16, 2016 pm 08:22 PM
php greatest common divisor algorithm calculate

本文实例讲述了php计算两个整数的最大公约数常用算法。分享给大家供大家参考。具体如下:

复制代码 代码如下:
//计时,返回秒
function  microtime_float ()
{
    list( $usec ,  $sec ) =  explode ( " " ,  microtime ());
    return ((float) $usec  + (float) $sec );
}
//////////////////////////////////////////
//欧几里得算法
function ojld($m, $n) {
    if($m ==0 && $n == 0) {
        return false;
    }
    if($n == 0) {
        return $m;
    }
    while($n != 0){
        $r = $m % $n;
        $m = $n;
        $n = $r;
    }
    return $m;
}
//////////////////////////////////////////
//基于最大公约数的定义
function baseDefine($m, $n) {
    if($m ==0 && $n == 0) {
        return false;
    }
    $min = min($m, $n);
    while($min >= 1) {
        if($m % $min == 0){
            if($n % $min ==0) {
                return $min;
            }
        }
        $min -= 1;
    }
    return $min;
}
////////////////////////////////////////////
//中学数学里面的计算方法
function baseSchool($m, $n) {
    $mp = getList($m); //小于$m的全部质数
    $np = getList($n); //小于$n的全部质数
    $mz = array();  //保存$m的质因数
    $nz = array();  //保存$n的质因数
    $mt = $m;
    $nt = $n;
    //m所有质因数
    //遍历m的全部质数,当能够被m整除时,继续下一次整除,知道不能被整除再取下一个能够被m整除
    //的质数,一直到所有出现的质数的乘积等于m时停止
    foreach($mp as $v) {
        while($mt % $v == 0) {
            $mz[] = $v;
            $mt = $mt / $v;
        }
        $c = 1;
        foreach($mz as $v) {
            $c *= $v;
            if($c == $m){
                break 2;
            }
        }
    }
    //n所有质因数
    foreach($np as $v) {
        while($nt % $v == 0) {
            $nz[] = $v;
            $nt = $nt / $v;
        }
        $c = 1;
        foreach($nz as $v) {
            $c *= $v;
            if($c == $n){
                break 2;
            }
        }
    }
    //公因数
    $jj = array_intersect($mz, $nz); //取交集
    $gys = array();
    //取出在俩数中出现次数最少的因数,去除多余的。
    $c = 1; //记录数字出现的次数
    $p = 0; //记录上一次出现的数字
    sort($jj);
    foreach($jj as $key => $v) {
        if($v == $p) {
            $c++;
        }
        elseif($p != 0) {
            $c = 1;
        }
        $p = $v;
        $mk = array_keys($mz, $v);
        $nk = array_keys($nz, $v);
        $k = ( count($mk) > count($nk) ) ? count($nk) : count($mk);
        if($c > $k) {
            unset($jj[$key]);
        }
    }
    $count = 1;
    foreach($jj as $value) {
        $count *= $value;
    }
    return $count;
}
//求给定大于等于2的整数的连续质数序列
//埃拉托色尼筛选法
function getList($num) {
    $a = array();
    $a = array();
    for($i = 2; $i         $a[$i] = $i;
    }
    for( $i = 2; $i         if($a[$i] != 0) {
            $j = $i * $i;
            while($j                 $a[$j] = 0;
                $j = $j + $i;
            }
        }
    }
    $p = 0;
    for($i = 2; $i         if($a[$i] != 0) {
            $L[$p] = $a[$i];
            $p++;
        }
    }
    return $L;
}
/////////////////////////////////////
//test
$time_start  =  microtime_float ();
//echo ojld(60, 24);       //0.0000450611 seconds
//echo baseDefine(60, 24); //0.0000557899 seconds
echo baseSchool(60, 24);   //0.0003471375 seconds
$time_end  =  microtime_float ();
$time  =  $time_end  -  $time_start ;
echo '
' . sprintf('%1.10f', $time) . 'seconds';

希望本文所述对大家的php程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles