php 求质素(素数) 的实现代码

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?php
class timer
{
var $time_start;
var $time_end;

function __construct()
{
$this->time_start = 0;
$this->time_end = 0;
}

function timer()
{
$this->__construct();
}

function start()
{
list($usec,$sec) = explode(" ",microtime());
$this->time_start = (float)$usec + (float)$sec;
}

function stop()
{
list($usec,$sec) = explode(" ",microtime());
$this->time_end = (float)$usec + (float)$sec;
}

function show($output = false)
{
$total = $this->time_end - $this->time_start;
if ($output) {
echo $total," sec";
return true;
}
return $total." sec";
}

}
?>
<?php
echo 'check prime<br/>';
function IsPrime($i)
{
if($i<2)
{
return false;
}
//var $iterator;
for($iterator = 2 ; $iterator <= sqrt($i) ; $iterator++)
{
if($i % $iterator==0)
{
return false;
}
}
return true;
}

$sw=new timer();
$sw->start();
for($j=1;$j<100;$j++)
{
if(IsPrime($j))
{
echo 'true<br/>';
}
else
{
echo 'false<br/>';
}
}
$sw->stop();
$sw->show(true);

?>

相关文章

php读取torrent种子文件内容的方法(测试可用)

本文实例讲述了php读取torrent种子文件内容的方法。分享给大家供大家参考,具体如下: <?php /** * Class xBEncoder * Author...

PHP耦合设计模式实例分析

本文实例分析了PHP耦合设计模式。分享给大家供大家参考,具体如下: 一个软件,它具有许多类,类与类之间需要互相调用,一旦某个类与另一个类具有紧密耦合关系的时候,这个软件的重用性就会大大降...

php中get_defined_constants函数用法实例分析

本文实例讲述了php中get_defined_constants函数用法。分享给大家供大家参考。具体分析如下: get_defined_constants ( PHP 4中 > =...

php 高效率写法 推荐

0、用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译...

php获取从百度搜索进入网站的关键词的详细代码

分享一个php获取从百度搜索进入网站的关键词的代码,有需要的朋友可以参考一下: 代码: 复制代码 代码如下: <?php function search_word_from() {...