首页 > 其他 > 详细

素数的写入

时间:2019-12-10 22:53:00      阅读:110      评论:0      收藏:0      [点我收藏+]
/*
7. 将1-100之间所有的质数, 按照5个一行,中间用水平制表符(\t)
隔开的格式写入到项目下的: temp\\num2.txt文件中.
例如:
2 3 5 7 11
13 17 19 23 29
分析质数的话,就是除了1和它本身之外没有别的因数
...
*/
public class Seven {
public static void main(String[] args) throws IOException {
ArrayList<Integer> ints = new ArrayList<>();
BufferedWriter bw = new BufferedWriter(new FileWriter("shushu.txt"));
int i,count = 0;
for (i = 2;i <= 100; i++) {
if(isPrime(i) == true){
ints.add(i);
}
}
for (Integer ani : ints) {
bw.write(ani + "\t");
bw.flush();
count++;
if(count % 5 == 0){
bw.write("\n");

}
}

bw.close();

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

}

素数的写入

原文:https://www.cnblogs.com/YRSWBY2016/p/12019584.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!