php 求质素(素数) 的实现代码
作者:bea
代码如下: <?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
代码如下:
<?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);
?>
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 关于session在PHP5的配置文件中的详细设置参数说明
- PHP session有效期session.gc_maxlifetime
- SWFUpload与CI不能正确上传识别文件MIME类型解决方法分享
- PHP中break及continue两个流程控制指令区别分析
- php中显示数组与对象的实现代码
- PHP的变量总结 新手推荐
- 写php分页时出现的Fatal error的解决方法
- zend api扩展的php对象的autoload工具
- Drupal 添加模块出现莫名其妙的错误的解决方法(往往出现在模块较多时)
- ThinkPHP采用模块和操作分析
- PHP学习之数组值的操作
- PHP学习之数组的定义和填充
- PHP学习之正则表达式
- PHP学习之字符串比较和查找
- PHP学习之整理字符串
- PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
- php 生成文字png图片的代码
- 适用于php-5.2 的 php.ini 中文版[金步国翻译]
- php编写一个简单的路由类