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

import java.io.*; import java.util.*; import java.lang.*; public class PrimeNumb

ID: 3638914 • Letter: I

Question

import java.io.*;
import java.util.*;
import java.lang.*;


public class PrimeNumbers1 {
public static void main(String[] args){
  int[] array = new int[10000];
  int odd = 3;
  array[0] = 2;   
  
  for(int i=1; i<array.length; i++){
   array[i] = odd;
   odd+=2;
  }
  
  //eliminate the non-prime
  for(int i=0; i<array.length; i++){
   if(array[i] < array[array.length-1]/2){
    for(int j=i+1; j<array.length; j++)
     if(array[j] != 0 && array[j] % array[i] == 0){
      array[j] = 0;
     }
   }
   else break;
  }
  //eliminate if the any of the number is even
  String fnum;
  for(int i=1; i<array.length; i++){ //skip 2, because its the only allowed one
   if(containEven(array[i])){
    array[i] = 0;
   }
  }
  long total = 0;
  
  try{
   BufferedWriter out = new BufferedWriter(new FileWriter("primes.txt"));
   for(int i=2; i<array.length; i++)
    if(array[i] != 0){
     out.write(array[i] + " ");
     total++;
    }
   out.close();
  }catch(IOException e){
                    }
  
  
  long end = System.currentTimeMillis();
  


}

public static boolean containEven(int num){
  String numString = Integer.toString(num);
  int stringLength = numString.length();
  for(int i=0; i<stringLength; i++){
   if(Integer.parseInt(Character.toString(numString.charAt(i))) % 2 == 0){
    return true;
   }
  }
  return false;
}
}

The Problem: Write a program that finds all prime numbers up to 10, 000, 000, 000. There are approximatley 455,052,511 such prime numbers. Your program should store the prime numbers in a binary file named Exercise23_8.dat. When a new prime is found it is appended to the file. To find wether a number is prime your program should load prime numbers from file to an array of the long type of size 10000. If no numbers in the array is a divisor for the new number continue to read the next 10000 prime numbers from the data file until the divisor is found or all numbers in the file are read. If no number is found the number is prime. I really want to understand this stuff so if you could post a detailed explanation to everything you do. BTW I found this program online I believe it to be correct but I dont understand it compleley so depending on whether it is correct you may not even have to write code but just explain to me this code.

Explanation / Answer

public class PrimeNumber { public static void main(String args[]) { int num=4; int count=0; System.out.println("First 10, 000, 000, 000 Prime Number is: "); System.out.println("1"); System.out.println("2"); System.out.println("3"); while(count
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote