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 采集心得技巧

1.获取远程文件源代码(file_get_contents或用fopen). 2.分析代码得到自己想要的内容(这里用正规匹配,一般是得到分页)。 3.跟根得到的内容进行下载入库等操作。...

PHP获取文件扩展名的4种方法

本文实例讲述了PHP获取文件扩展名的4种方法。分享给大家供大家参考,具体如下: $filename="123.jpg"; //方法一: function get_ext($file_...

解决FastCGI 进程超过了配置的活动超时时限的问题

解决FastCGI 进程超过了配置的活动超时时限的问题

近日,需要满足测试需求,进行大数据并发测试时,报出【HTTP 错误 500.0 - Internal Server Error E:\PHP\php-cgi.exe - FastCGI...

php获取QQ头像并显示的方法

本文实例讲述了php获取QQ头像并显示的方法。分享给大家供大家参考。具体分析如下: 最近看到博客留言的头像有点别扭,因为游客的头像都是同一个头像,看着不是很舒服。虽然现在绝大多数的主题集...

PHP实现websocket通信的方法示例

本文实例讲述了PHP实现websocket通信的方法。分享给大家供大家参考,具体如下: 执行方法: 首先先修改server.php与index.html的ip 通过命令行执行 [php路...