|
| 1 | +package ch_22.exercise22_08; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * *22.8 (All prime numbers up to 10,000,000,000) Write a program that finds |
| 7 | + * all prime numbers up to 10,000,000,000. There are approximately |
| 8 | + * 455,052,511 such prime numbers. Your program should meet the following |
| 9 | + * requirements: |
| 10 | + * ■ Your program should store the prime numbers in a binary data file, named |
| 11 | + * PrimeNumbers.dat. When a new prime number is found, the number is |
| 12 | + * appended to the file. |
| 13 | + * ■ To find whether a new number is prime, your program should load the |
| 14 | + * prime numbers from the file to an array of the long type of size 10000. |
| 15 | + * If no number in the array is a divisor for the new number, continue to read |
| 16 | + * the next 10000 prime numbers from the data file, until a divisor is found |
| 17 | + * or all numbers in the file are read. If no divisor is found, the new number |
| 18 | + * is prime. |
| 19 | + * ■ Since this program takes a long time to finish, you should run it as a batch |
| 20 | + * job from a UNIX machine. If the machine is shut down and rebooted, your |
| 21 | + * program should resume by using the prime numbers stored in the binary data |
| 22 | + * file rather than start over from scratch. |
| 23 | + */ |
| 24 | +public class Exercise22_08 { |
| 25 | + private static PrimeStorage primeStorage; |
| 26 | + private static final String[] packageParts = Exercise22_08.class.getPackage().getName().split("\\."); |
| 27 | + private static final String PATH = packageParts[0] + File.separator + packageParts[1] + File.separator + "PrimeNumbers.dat"; |
| 28 | + private static long[] primeReadBuffer = new long[10000]; |
| 29 | + private static final long UPPER_BOUND = 10_000_000_000L; |
| 30 | + private static boolean reachedEndOfBuffer = false; |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + try { |
| 34 | + primeStorage = new PrimeStorage(PATH); |
| 35 | + long checkNum = primeStorage.getNextNumberToCheck(); |
| 36 | + // Check primes until reaching upper bound |
| 37 | + while (checkNum < UPPER_BOUND) { |
| 38 | + boolean isPrime = checkPrime(checkNum, primeStorage); |
| 39 | + if (isPrime) { |
| 40 | + primeStorage.appendPrime(checkNum); |
| 41 | + } |
| 42 | + checkNum++; |
| 43 | + } |
| 44 | + |
| 45 | + } catch (Exception e) { |
| 46 | + System.err.println("Could not create and/or read/write file needed to store prime numbers on this device. " + |
| 47 | + "Please confirm Security Manager will allow create/read/write file storage."); |
| 48 | + System.exit(0); |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + private static boolean checkPrime(long checkNum, PrimeStorage primeStorage) throws IOException { |
| 54 | + boolean loop = true; |
| 55 | + boolean isPrime = true; |
| 56 | + primeStorage.setPosition(0); |
| 57 | + |
| 58 | + while (loop) { |
| 59 | + primeReadBuffer = primeStorage.readNextPrimes(); |
| 60 | + boolean isDivisible = isDivisible(primeReadBuffer, checkNum); |
| 61 | + if (isDivisible) { |
| 62 | + // checkNum is not prime |
| 63 | + loop = false; |
| 64 | + isPrime = false; |
| 65 | + break; |
| 66 | + } else if (reachedEndOfBuffer) { |
| 67 | + loop = false; |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return isPrime; |
| 73 | + } |
| 74 | + |
| 75 | + public static boolean isDivisible(long[] primes, long checkNum) { |
| 76 | + for (long prime : primes) { |
| 77 | + if (prime == 0) { |
| 78 | + reachedEndOfBuffer = true; |
| 79 | + return false; |
| 80 | + } |
| 81 | + if (checkNum % prime == 0) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + return false; |
| 86 | + } |
| 87 | +} |
0 commit comments