时间:2021-07-01 10:21:17 帮助过:38人阅读

php求不大于n的质数:
使用循环遍历、当为质数时加入list中
public class Test4 {
public static void main(String[] args) {
Test4 t=new Test4();
List l=t.getAll(5);
Iterator it=l.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public List<Integer> getAll(int n){
List<Integer> prime=new ArrayList<Integer>();
for(int i=n;i>1;i--){
boolean flag=true;
for(int j=i-1;j>1;j--){
if(i%j==0){
flag=false;
break;
}
}
if(flag){
prime.add(i);
}
}
return prime;
}
}推荐:php服务器
以上就是php求不大于n的质数的详细内容,更多请关注Gxl网其它相关文章!