PHP质数判断

来源:互联网 发布:mac 地址 库 编辑:程序博客网 时间:2024/05/17 00:51
求N以内的质数之和:

代码:
<?php
/*
*  Author : Fanglor
*  Email  : Fanlor@163.com
*  Url    : www.skyleft.com
*  Date   : 2009-8-22
*/
//质数定义: 一个数,有且只能被1与其本身整除.
function _test ($n) {
$temp = 0;
for ($i=1;$i<=$n;$i++) {
$num =0;
for ($j=1;$j<=$i;$j++) {

if (!($i%$j)) {
$num++;
}
}
if ($num <= 2) {
echo $i.'<br/>';
$temp+=$i;
}
}
return $temp;
}
echo _test (13);
?>

打印结果:
1
2
3
5
7
11
13
42
原创粉丝点击