100亿以内的素数,利用文本读写保存,以在保存的位置继续

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;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你这个年纪你是怎么睡得着的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值