php求不大于n的质数-PHP问题

资源魔 40 0

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教程 php故障解决 php使用问题 质数

抱歉,评论功能暂时关闭!