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

yipeiwu_com5年前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中spl_autoload_register函数的用法总结

spl_autoload_register(PHP 5 >= 5.1.2)spl_autoload_register — 注册__autoload()函数 说明bool spl_a...

XHProf报告字段含义的解析

Function Name:方法名称。 Calls:方法被调用的次数。 Calls%:方法调用次数在同级方法总数调用次数中所占的百分比。 Incl.Wall Time(microsec)...

php中邮箱地址正则表达式实现与详解

首先附上代码 复制代码 代码如下: ^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$ 在这段正则表达式中,“+”表示前面的字符串连续出现一...

浅析PHP递归函数返回值使用方法

PHP经过长时间的发展,很多用户都很了解PHP了,PHP最初是1994年Rasmus Lerdorf创建的,刚刚开始只是一个简单的用Perl语言编写的程序,用来统计他自己网站的访问者。后...

PHP中使用imagick实现把PDF转成图片

PHP中使用imagick实现把PDF转成图片

PHP Manual里,对imagick的描述,真的是简洁,每个成员函数,点击打开就看到如下文本: 复制代码 代码如下: Warning This function is current...