***PLEASE MAKE SURE THIS CODE RUNS IN NETBEANS JAVA SE*** Part 2. Implement a me
ID: 3807309 • Letter: #
Question
***PLEASE MAKE SURE THIS CODE RUNS IN NETBEANS JAVA SE***
Part 2. Implement a method that open file PrimeNumbers.dat and perform the follwoing:
22.8 (All prime numbers up to 10,000,000,000) Write a program that finds all prime numbers up to 10,000,000,000. There are approximately 455.052.511 such prime numbers. Your program should meet the following Your program should store the prime numbers in a binary data file, named PrimeNumbersdat. When a new prime number is found, the number is appended to the file. To find whether a new number is prime, your program should load the prime numbers from the file to an array of the long type of size 10000. If no number in the array is a divisor for the new number, continue to read the next 10000 prime numbers from the data file, until a divisor is found or all numbers in the file are read. If no divisor is found, the new number is prime.Explanation / Answer
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Prime {
public static void main(String[] args) throws IOException {
DataInputStream input = new DataInputStream(new FileInputStream(
"data.dat"));
long[] data = new long[10000];
System.out.println("Enter the new number: ");
long num = new Scanner(System.in).nextLong();
for(int i=0;i<10000;i++){
while (input.available() > 0) {
data[i] = input.readLong();
System.out.println(data[i]);
}
if(num % data[i] == 0)
System.out.println("New Number is not Prime");
else
System.out.println("New Number is Prime Number");
}
input.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.