📄 shiyan9_6.java
字号:
import java.io.*;
public class shiyan9_6
{
RandomAccessFile bin;
public static void main(String args[]) throws IOException
{
shiyan9_6 s=new shiyan9_6();
s.createprime(100);
}
public void createprime(int max) throws IOException
{
bin=new RandomAccessFile("F:\\123java\\primes.bin","rw");
bin.seek(0);
bin.writeInt(2);
int k=3;
while (k<=max)
{
if(isPrime(k))
bin.writeInt(k);
k=k+2;
}
output(max);
bin.close();
}
public boolean isPrime(int k) throws IOException
{
int i=0,j;
boolean yes = true;
try
{
bin.seek(0);
int count = (int)(bin.length()/4);
while ((i<=count)==yes)
{
if (k % bin.readInt()==0)
yes=false;
else
i++;
bin.seek(i*4);
}
}catch(IOException e){}
return yes;
}
public void output(int max) throws IOException
{
try
{
bin.seek(0);
System.out.println("[2---"+max+"]中有"+(bin.length()/4)+"个素数:");
for(int i=0;i<(int)(bin.length()/4);i++)
{
bin.seek(i*4);
System.out.print(bin.readInt()+" ");
if((i+1)%10==0)
System.out.println();
}
}catch(IOException e){}
System.out.println();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -