手机
当前位置:查字典教程网 >编程开发 >php教程 >php 求质素(素数) 的实现代码
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 求质素(素数) 的实现代码】相关文章:

php将HTML表格每行每列转为数组实现采集表格数据的方法

php解析xml方法实例详解

PHP中把对象转换为关联数组代码分享

php中PDO方式实现数据库的增删改查

php快速查找数据库中恶意代码的方法

php注入实例

文件上传的实现

php数组合并与拆分实例分析

无限级别菜单的实现

我的论坛源代码(九)

精品推荐
分类导航