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]实用函数8

//建立dBase资料表 int dBase_create(string filename,array fields) //打开dBase资料表 int&n...

PHP输入流php://input实例讲解

对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述。 “php://input allows you to read raw POST data. It i...

PHP 文件上传进度条的两种实现方法的代码

目前我知道的方法有两种,一种是使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/packa...

php抽象类使用要点与注意事项分析

本文实例分析了php抽象类使用要点与注意事项。分享给大家供大家参考。具体分析如下: php抽象类使用要点与注意事项如下: 1、用 abstract 来修饰一个类,那么这个类就是抽象类;抽...

PHP 7的一些引人注目的新特性简单介绍

1. ?? 运算符(NULL 合并运算符) 把这个放在第一个说是因为我觉得它很有用。用法: $a = $_GET['a'] ?? 1; 它相当于:...