📄 neatprimesoutput.java
字号:
import java.io.*;
public class NeatPrimesOutput
{
public static void main(String[] args)
{
long[] primes = new long[20]; // Array to store primes
primes[0] = 2; // Seed the first prime
primes[1] = 3; // and the second
int count = 2; // Count of primes found - up to now
// which is also the array index
long number = 5; // Next integer to be tested
outer:
for( ; count < primes.length; number += 2)
{
// 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!
}
// Output the primes array using a formatted buffered stream
FormatWriter out = new FormatWriter(
new BufferedWriter(
new FileWriter(FileDescriptor.out)), 12);
for(int i=0; i < primes.length; i++)
{
if(i%5==0) // New line before every fifth prime
out.println();
out.print(primes[i]); // Output a prime
}
out.close(); // Close the stream
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -