![](https://img.php.cn/upload/article/000/000/039/5db7d5ccef4aa783.jpg)
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中文网其它相关文章!
![php中文网最新课程二维码](/static/images/article_wechat2021.jpg?1111)
声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论