教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php求不大于n的质数

php求不大于n的质数

发布时间:2020-06-20   编辑:jiaochengji.com
教程集为您提供php求不大于n的质数等资源,欢迎您收藏本站,我们将为您提供最新的php求不大于n的质数资源

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求不大于n的质数
    php求1000以内质数(素数)
    php质数算法简单示例
    php 质数计算 求100以内质数和
    php模拟多线程的实例分享
    php如何写一个数是不是质数
    PHP 素数计算算法示例
    使用PHP求最大奇约数的和
    php 矩阵转置 求素数 冒泡排序 选择排序例子
    python输出2到100之间的素数

    [关闭]
    ~ ~