输出1到n以内的素数

Posted on 2017-11-08 19:37  lhj1006400320  阅读(246)  评论(0编辑  收藏  举报
package cn.lhj.learn;

/**
 * 输出1~n以内的素数
 * 
 * @author lhj
 *
 */
public class TestSuShu {

    public static void main(String[] args) {
        for (int i = 2; i <= 100; i++) {
            if (find(i)) {
                System.out.print(i + " ");
            }
        }
    }

    public static boolean find(int n) {
        for (int i = 2; i < Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

}

 

Copyright © 2024 lhj1006400320
Powered by .NET 8.0 on Kubernetes