?? tryprimesoutput.java
字號:
import java.io.*;
public class TryPrimesOutput
{
public static void main(String[] args)
{
long[] primes = new long[200]; // Array to store primes
primes[0] = 2; // Seed the first prime
primes[1] = 3; // and the second
int count = 2; // count of primes found so far
long number = 5; // Next integer to be tested
outer:
for( ; count < primes.length; number += 2L)
{
// The maximum divisor we need to try is square root of number
long limit = (long)Math.ceil(Math.sqrt((double)number));
// Divide by all the primes we have up to limit
for(int i = 1; i < count && primes[i] <= limit; i++)
if(number%primes[i] == 0) // Is it an exact divisor?
continue outer; // yes, try the next number
primes[count++] = number; // We got one!
}
// Write the primes to a file
try
{
String dirName = "c:\\JunkData"; // Directory for the file
String fileName = "Primes.bin"; // The file name
File myPrimeDir = new File(dirName); // Define directory object
if(!myPrimeDir.exists()) // If directory does not exist
myPrimeDir.mkdir(); // ...create it
else
if(!myPrimeDir.isDirectory()) // Verify it is a directory
{ // It is not
System.err.println(dirName+" is not a directory");
return;
}
// Create the file object
File primesFile = new File(myPrimeDir, fileName);
primesFile.createNewFile(); // If it doesn't exist, create it
// Create a buffered data output stream for the file
DataOutputStream primesStream = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(primesFile)));
// Write primes to the file
for(int i = 0; i < primes.length; i++)
primesStream.writeLong(primes[i]);
primesStream.close(); // Flush and close the file
System.out.println("File size = " + primesStream.size());
}
catch(IOException e) // Catch any output errors
{
System.out.println("IOException " + e + " occurred");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -