Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Problem Create a JAVA program that generates a file of all the prime numbers l

ID: 3698004 • Letter: 1

Question

1 Problem Create a JAVA program that generates a file of all the prime numbers less than a number N taken from command line. It is up to you how you store this data into the file as long as your next problem can read it. Your program should work for numbers up to 100,000. You must implement the Sieve of Eratosthenes in order to generate this file. You are welcome to take a look at the wikipedia source on this which even includes some pseudocode https://en.wikipedia.org/ wiki/Sieve_of_Eratosthenes

Example input: 10

Example output in ‘‘file.txt’’: 2 3 5 7

Problem 2 Create a program that takes a file of prime numbers generated from Problem 1 and stores them in any way of your choosing. The user may query the program as many times as they would like and it will output whether or not the number given is prime. The queries must run in log(n) time where n is the amount of prime numbers in the file. If the number that is entered is negative stop the program. If the number is greater than the largest in the file inform the user that they need a file which contains more prime numbers.

Example input in ‘‘file.txt’’: 2 3 5 7

Example command line interaction with user: Enter a number. 3

3 is a prime number. Enter another number. 4

4 is not a prime number. Enter another number. 8

Current file not large enough for 8. Enter another number.

-1

Bye.

Explanation / Answer

Problem 1:

PrimeNumbers.java


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class PrimeNumbers {

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException {
       // TODO Auto-generated method stub
       Scanner scan = new Scanner(System.in);
       FileWriter fw = new FileWriter(new File("D:\file.txt"));
       System.out.println("Please enter n value : ");
       int n = scan.nextInt();
       String s = "";
       int num =0;
       for(int i=2; i<n && num < n ; i++){
           int counter=0;   
   for(num =i; num>=1; num--)
       {
   if(i%num==0)
       {
          counter = counter + 1;
       }
       }
       if (counter == 2)
       {
           if(s == "")
           s = ""+i;
           else          
           s = s + " " +i;
           System.out.print(i + " ");
       }
       }
       fw.write(s);
       fw.flush();
       fw.close();
       scan.close();
   }

}

Output:

Please enter n value :
10
2 3 5 7

Problem 2:

PrimeNumbersSearch.java


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class PrimeNumbersSearch {

   /**
   * @param args
   * @throws FileNotFoundException
   */
   public static void main(String[] args) throws FileNotFoundException {
       // TODO Auto-generated method stub
       File file = new File("D:\file.txt");
       Scanner scan = new Scanner(file);
       Scanner scan1 = new Scanner(System.in);
       if(file.exists()){
           String s = scan.nextLine();
           String values[] = s.split(" ");
           System.out.println("Enter a number");
           boolean status = false;
           while(true){
               int num = scan1.nextInt();
               status = false;
               if(Integer.parseInt(values[values.length-1]) < num){
                   System.out.println("Current file not large enough for "+num);
               System.out.println("Enter another number");
               }
               else{
               if(num < 0) {
                   System.out.println("Bye");
                   break;
               }
               for(int i=0; i<values.length; i++){
                   if(num == Integer.parseInt(values[i])){
                       System.out.println(num+ " is a prime number.");
                       System.out.println("Enter another number");
                       status = true;
                       break;
                   }
               }
               if(status == false){
                   System.out.println(num+ " is not a prime number");
                   System.out.println("Enter another number");
               }
               }
           }
       }
       else{
           System.out.println("Input File does not exist");
       }
   }

}

Output:

Enter a number
3
3 is a prime number.
Enter another number
4
4 is not a prime number
Enter another number
8
Current file not large enough for 8
Enter another number
-1
Bye