![](http://m.sbmmt.com/img/upload/article/000/000/039/5db7d5ccef4aa783.jpg)
php入门到就业线上直播课:进入学习
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的质数的详细内容,更多请关注php中文网其它相关文章!
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
前端(VUE)零基础到就业课程:点击学习
自己动手写 PHP MVC 框架:点击学习
![](http://m.sbmmt.com/img/upload/article/000/000/020/633501a29d313483.png)