package com.prime;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.util.Date;
public class Prime {
protected static final int BUFFER_SIZE = 16 * 1024;
public BufferedWriter bfWriteFile = null;
private static String pathFile = "d://Prime//index.txt";
private static String path = "D://Prime//";
static String crlf = System.getProperty("line.separator");
public static void main(String[] args) {
long l1 = System.currentTimeMillis();
System.out.println("開始:" + new Date(l1).toLocaleString() );
int N = 15000000;
int[] sieve = new int[N + 1];
int i = 0;
for (i = 2; i <= N; i++)
sieve[i] = 1;
for (i = 2; i <= N / 2; i++)
sieve[i * 2] = 0;
int p = 2;
while (p * p <= N) {
p = p + 1;
while (sieve[p] == 0)
p++;
int t = p * p;
int s = 2 * p;
while (t <= N) {
sieve[t] = 0;
t = t + s;
}
}
String s = "";
int j = 0;
try {
BufferedOutputStream out = null;
out = new BufferedOutputStream(new FileOutputStream(pathFile, true), BUFFER_SIZE);
for (i = 2; i <= N; i++) {
if (i % 100 == 0)s += crlf;
if (sieve[i] != 0) {
j++;
s += i + ",";
if (j % 1000 == 0) {
if(j % 10000 == 0){
out.close();
out = new BufferedOutputStream(
new FileOutputStream(path + "index_" + j + ".txt" , true), BUFFER_SIZE);
}
out.write(s.getBytes());
s = "";
System.out.println(i);
}
}
}
out.close();
} catch (Exception e) {
// TODO: handle exception
}
long l2 = System.currentTimeMillis();
System.out.println("結束:" + (new Date(l2)).toLocaleString());
System.out.println(":" + (l2 -l1));
}
}