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);

?>

相关文章

input file获得文件根目录简单实现

input file获得文件根目录简单实现

其实问题很简单 index.html 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E...

PHP安全性漫谈

PHP安全性漫谈

一、apache server安全性设置 1、以Nobody用户运行 一般情况下,Apache是由Root 来安装和运行的。如果Apache Server进程具有Root用户特权,那么它...

php身份证号码检查类实例

本文实例讲述了php身份证号码检查类。分享给大家供大家参考。具体如下: <?php class CIDMaker // 声明一个身份证号码检查类 { var $i...

php5.3后静态绑定用法详解

php5.3后静态绑定用法详解

本文实例讲述了php5.3后静态绑定用法。分享给大家供大家参考,具体如下: 手册原文: 自 PHP 5.3.0 起,PHP 增加了一个叫做后期静态绑定的功能,用于在继承范围内引用静态调用...

PHP常用的排序和查找算法

本文汇总了常见的php排序算法和查找,在进行算法设计的时候有不错的借鉴价值。现分享给大家供参考之用。具体如下: <?php /** * PHP最常用的四个排序方法及二...