网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/9/25 浏览:3 次 )
复制代码 代码如下:
<?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编写一个简单的路由类
下一篇:php 5.3.5安装memcache注意事项小结
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap