$end
=
count
(
$arr
)-1;
$i
= 0;
while
(
$start
<=
$end
) {
$i
++;
$step
=
floor
(
$end
/ 2);
if
(
$a
==
$arr
[
$step
])
{
print_r(
$arr
[
$step
]);
return
$a
;
}
if
(
$a
>
$arr
[
$step
])
{
$start
=
$step
;
}
if
(
$a
<
$arr
[
$step
])
{
$end
=
$step
;
}
}
}
}
php
–
求素数
计算 a
到 b
之间的素数。 代码示例
复制代码 代码如下:
//php
求素数 -
计算 a
到 b 之间的素数。
function
sushu(
$a
,
$b
)
{
if
(!
empty
(
$a
) && !
empty
(
$b
))
{
if
(
$b
<
$a
)
return
;
$temp
=
array
();
for
(
$i
=
$a
;
$i
<=
$b
;
$i
++)
{
$j
=
intval
(sqrt(
$i
));
$flag
= true;
if
(
$i
<=3)
{
$temp
[
$i
] =
$i
;
}
else
{
for
(
$x
=2;
$x
<=
$j
;
$x
++)
{