package lab.day01;
import java.io.*;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class lab22_8 {
/**
* 实时读取指定文件的内容
*
* @param file
* @throws FileNotFoundException
*/
static long geShu = 0; // 素数个数
static long weiZhi = 0; // 位置是哪一个Sweizhi
static long SweiZhi = 0; // 一个代表100000个数
static long zongPanDuan = 100000; // 一次性判断100000个
static long num = 2; // 素数
static long count = 1; // 用与分割
// static Long[] fanHui;
// static List<Long> baoCun = new ArrayList<>();
static LinkedList<Long> baoCun = new LinkedList<>();
// 使用LinkedList 可以输出链表的最后一个值 用 getLast()方法
// static List<Long> baoCunC = new ArrayList<>();
static File file = new File("PrimeNumber.txt");
public static void main(String[] args) throws FileNotFoundException {
// file.delete();
while (weiZhi < 100000) {
search(file);
}
readPrint(file);
}
public static Long[] read(File file) throws FileNotFoundException {
try (Scanner input = new Scanner(file)) {
input.useDelimiter(" ");//设置读取分隔符
while (input.hasNext()) {
baoCun.add(input.nextLong());
}
SweiZhi = baoCun.getLast() / zongPanDuan + 1;
Long[] fanHui = baoCun.toArray(new Long[baoCun.size()]);
// System.out.println(Arrays.toString(fanHui));
return fanHui;
}
}
public static Long[] readPrint(File file) throws FileNotFoundException {
try (Scanner input = new Scanner(file)) {
input.useDelimiter(" ");//设置读取分隔符
while (input.hasNext()) {
baoCun.add(input.nextLong());
}
SweiZhi = baoCun.size() / zongPanDuan + 1;
Long[] fanHui = baoCun.toArray(new Long[baoCun.size()]);
System.out.println(Arrays.toString(fanHui));
return fanHui;
}
}
public static void write(long i, File file) throws FileNotFoundException {
//启动一个线程每1秒钟向日志文件写一次数据
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.scheduleWithFixedDelay(new Runnable() {
public void run() {
try {
// if(file == null) {
//
// throw new IllegalStateException("logFile can not be null!");
//
// }
Writer txtWriter = new FileWriter(file, true);
// txtWriter.write(dateFormat.format(new Date()) +"\t"+ i +"\n");
txtWriter.write(i + " ");
txtWriter.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}, 0, 1, TimeUnit.SECONDS);
}
public static void search(File file) throws FileNotFoundException {
if (file.exists()) {
Long[] fanHui = read(file);
searchHou(fanHui, file);
} else {
searchYi(file);
}
}
public static void searchYi(File file) throws FileNotFoundException {
boolean[] primes = new boolean[100001];
for (int i = 0; i < primes.length; i++) {
primes[i] = true;
}
for (num = 2; num <= 100000 / num; num++) {
if (primes[(int) num]) {
// System.out.println(num + "c");
for (long i = num; i <= 100000 / num; i++) {
// System.out.println(num * i);
// System.out.println(i + "a");
primes[(int) (num * i)] = false;
// System.out.println(primes[num * i]);
}
// System.out.println(num + "b");
}
}
for (int i = 2; i < primes.length; i++) {
if (primes[i]) {
// System.out.println(primes[i]);
write(i, file);
// System.out.println(i + " ");
}
}
SweiZhi++;
}
// public static Long[] readC(Long i ,File file){
// baoCunC.add()
// }
public static void searchHou(Long[] fanHui, File file) throws FileNotFoundException {
long xianPanDuan = SweiZhi * zongPanDuan + 1;
// System.out.println(xianPanDuan);
while (xianPanDuan <= (SweiZhi + 1) * zongPanDuan) {
// for (int i = 0; i < zongPanDuan; i++){
long i = 0; // 下标 判断
// System.out.println(xianPanDuan + i);
// System.out.println(fanHui[i]);
// System.out.println(fanHui[i]);
// while (i < zongPanDuan) {
// System.out.println(Arrays.toString(fanHui));
while (i < fanHui.length) {
// if (i < fanHui.length) {
if (xianPanDuan % (fanHui[(int) i]) == 0) {
// System.out.println(fanHui[(int) i] + " b ");
xianPanDuan++;
i=0;
continue;
} else {
i++;
}
}
final int NUMBER_PER_LINE = 10;
if (count % NUMBER_PER_LINE == 0) {
System.out.println(xianPanDuan);
count = 1;
} else {
System.out.print(xianPanDuan + " ");
count++;
}
write(xianPanDuan, file);
break;
}
}
}
100亿以内的素数,利用文本读写保存,以在保存的位置继续
于 2022-10-25 18:15:31 首次发布