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

yipeiwu_com4年前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);

?>

相关文章

慎用preg_replace危险的/e修饰符(一句话后门常用)

preg_replace函数原型: mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int...

探讨fckeditor在Php中的配置详解

前言:  FCKeidtor是个国外的多语言编辑器,你可以对其配置文件进行简单修改使之支持目前常用Web开发语言的应用,下面我就讲讲FCKeditor的最新版本2.4.2在php的具体配...

discuz authcode 经典php加密解密函数解析

原理如下,假如: 加密 明文:1010 1001 密匙:1110 0011 密文:0100 1010 得出密文0100 1010,解密之需和密匙异或下就可以了 解密 密文:0100 10...

php5.3不能连接mssql数据库的解决方法

本文实例讲述了php5.3不能连接mssql数据库的解决方法。分享给大家供大家参考。具体分析如下: 自从php5.3之后系统就不支持mssql_connect这个函数连接了,以前我也讲可...

php基于协程实现异步的方法分析

本文实例讲述了php基于协程实现异步的方法。分享给大家供大家参考,具体如下: github上php的协程大部分是根据这篇文章实现的:http://nikic.github.io/2012...