首页 > 学院 > 开发设计 > 正文

java 找出100以内的素数

2019-11-17 04:04:35
字体:
来源:转载
供稿:网友
view plaincopy to clipboardPRint?
public class FindPrime {   
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        // TODO Auto-generated method stub   
        int num = 100;   
        String s = "100以内的素数:";   
        for (int i = 1; i <= num; i++) {   
            int count = 0;   
            for (int j = 1; j <= (int) Math.sqrt(i); j++) {   
                if (i % j == 0)   
                    count++;   
            }   
            if (count > 1)   
                s += "";   
            else  
                s += i + " ";   
        }   
        System.out.println(s);   
    }   
    //result   
    //100以内的素数:1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97    
}   
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表