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

Need help on my lab assignment, this is what I\'ve gotten so far and I am having

ID: 3836684 • Letter: N

Question

Need help on my lab assignment, this is what I've gotten so far and I am having a hard time understanding

Assignment is to print the prime numbers between 1-1000 while keeping a total count of those primes. Group the numbers 10 per line, and then also check if it is a Mersenne Prime (if it is flag it with "*") and keep a count for that as well. Finally, keep a count how many primes per hundreds. Also supposed to read and store the numbers into a text file once completed.

I've fallen behind in this class now and am having difficult wrapping my head around this and I've been working on trying to use methods to check the numbers - but am unsure if I was using it properly which led me to not calling it and using the formula instead

//imports
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
// **main class**

//Create an array that stores Prime Numbers between 1-1000 into each element
//Keep a count for Total Prime Numbers, Total Mersennes, and how Primes per Hundreds
//Display with 10 Per Line
//Check each element if Mersennes
//Read numbers and store into new data file

public class PrimesAndMers {
//static int count;
static boolean iMers;
    static int merscount = 0;
    static int hund1, hund2, hund3, hund4, hund5, hund6, hund7, hund8, hund9, hund10;
    static final int NUMBER_OF_PRIMES = 1000;
    static final int PER_LINE = 10;
    static int count=0;
    static int n=2;
   static final boolean $DEBUG = true;
   //main method
   public static void main (String[] args) {
         //debug
      if ($DEBUG) System.out.println ("debug::restarting code");
         //code starts here
      System.out.println(" Primes between 1 and 1000 are: ");
      System.out.println("-------------------------------------------------");
      int[] arr = new int [1000];

      for(int i=0;i<arr.length;i++){
   
       if(isPrime(n)){
        count++;
        arr[i]=n;

        if(count%PER_LINE==0){
          boolean iMers=false;  
          long mersennePrime = (long)(Math.pow(2, n))-1; //this is where I feel my issue is
          if((isPrime(n)==isPrime(mersennePrime))){  //cannot properly calculate & flag/replace/add "*"
          //if(isPrime(mersennePrime)){   //onto the MersennePrimes (3,7,31,127)
           iMers=true;     //also unsure about my isMers method
           merscount++;
          }
          if(iMers){
          // merscount++;
         System.out.print("*");
        }System.out.printf("%-5s ", n);
        }
        else System.out.printf("%-5s", n);
       }
          if (n > 0 && n < 100) {
              hund1 = count;
           }
           else if (n > 100 && n < 200) {
              hund2 = count - hund1;
           }
           else if (n > 200 && n < 300) {
              hund3 = count - hund2 - hund1;
           }
           else if (n > 300 && n < 400) {
              hund4 = count - hund3 - hund2 - hund1;
           }
           else if (n > 400 && n < 500) {
              hund5 = count - hund4 - hund3 - hund2 - hund1;
           }
           else if (n > 500 && n < 600) {
              hund6 = count - hund5 - hund4 - hund3 - hund2 - hund1;
           }
           else if (n > 600 && n < 700) {
              hund7 = count - hund6 - hund5 - hund4 - hund3 - hund2 - hund1;
           }
           else if (n > 700 && n < 800) {
              hund8 = count - hund7 - hund6 - hund5 - hund4 - hund3 - hund2 - hund1;
           }
           else if (n > 800 && n < 900) {
              hund9 = count - hund8 - hund7 - hund6 - hund5 - hund4 - hund3 - hund2 - hund1;
           }
           else if (n > 900 && n < 1000) {
              hund10 = count - hund9 - hund8 - hund7 - hund6 - hund5 - hund4 - hund3 - hund2 - hund1;
           }
      
       n++;
      }//merscount++;
     // System.out.print(arr.length);
      System.out.println(" -------------------------------------------------");
      System.out.println(" Total Primes are: "+count);
      System.out.println("Total Mersennes are: " + merscount);
      System.out.println("Primes per Hundreds are: ");
      System.out.println(hund1 + " , " + hund2 + " , " + hund3 + " , " + hund4 + " , " + hund5 + " , " + hund6 + " , " + hund7 + " , " + hund8 + " , " + hund9 + " , " + hund10);
     
   }
  
   public static boolean isPrime(long number){
    boolean iPrime=true;
    for(int d=2; d<=number/2;d++){
     if(n%d==0){
      iPrime=false;
      break;
     }
    }return iPrime;
   }
  
   public static boolean isMers (long number){
    boolean iMers=false;
    long mersennePrime = (long)(Math.pow(2, number))-1;
    if((isPrime(number)==isPrime(mersennePrime))){
     iMers=true;
    
    }return iMers;
   }

   public void fileOut() {
File fName2 = new File("Documents/OutFile.txt");
PrintWriter output = new PrintWriter(fName2);
String[] outArr = new String[100[;
for(int i=0; i<outArr.lengthl i++){
output.println(outArr[i]);

      //check CL parameter usage
      if(args.length !=4) {
         System.out.println("Usage: java ReplaceText sourceFile targetFile oldStr newStr");
         System.exit(1);
      }
       
        //check if source file exists
      File sourceFile = new File(args[0]);
      if(!sourceFile.exists()){
         System.out.println("Source file " + args[0] + " does not exist");
         System.exit(2);
      }
       
        //check if target file exists
      File targetFile = new File(args[1]);
      if(targetFile.exists()){
         System.out.println("Target file "+ args[1] + " already exists");
         System.exit(3);
      }
       
      try (
        //create input and output files
         Scanner input = new Scanner(sourceFile);
         PrintWriter output = new PrintWriter(targetFile);
      )
      {
         while(input.hasNext()){
            String s1 = input.nextLine();
            String s2 = s1.replaceAll(args[2], args[3]);
            output.println(s2);
         }
      }
   }
}
   }
}

Explanation / Answer

Sample Code:

//imports
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
// **main class**

//Create an array that stores Prime Numbers between 1-1000 into each element
//Keep a count for Total Prime Numbers, Total Mersennes, and how Primes per Hundreds
//Display with 10 Per Line
//Check each element if Mersennes
//Read numbers and store into new data file

public class HelloWorld {
   //static int count;
   static boolean iMers;
    static int merscount = 0;
    static int hund1, hund2, hund3, hund4, hund5, hund6, hund7, hund8, hund9, hund10;
    static final int NUMBER_OF_PRIMES = 1000;
    static final int PER_LINE = 10;
    static int count=0;
    static int n=2;
    static final boolean $DEBUG = true;
   //main method
   public static void main (String[] args) {
       //debug
       if ($DEBUG)
           System.out.println ("debug::restarting code");
       //code starts here
       System.out.println(" Primes between 1 and 1000 are: ");
       System.out.println("-------------------------------------------------");
       int[] arr = new int [1000];

       for(int i=0;i<arr.length;i++){

           if(isPrime(n)){
               count++;
               arr[i]=n;
               // break;

               //if(count%PER_LINE==0){
                   boolean iMers=false;
                   long mersennePrime = (long)(Math.pow(2, n))-1; //this is where I feel my issue is
                   if((isPrime(n) && isPrime(mersennePrime))){ //cannot properly calculate & flag/replace/add "*"
                       iMers=true;     //also unsure about my isMers method
                       merscount++;
                       System.out.print("*");
                       System.out.printf("%-5s ", n);
                   }
                   else System.out.printf("%-5s", n);
           //   }
               if (n > 0 && n < 100) {
                   hund1 = count;
               }
               else if (n > 100 && n < 200) {
                   hund2 = count - hund1;
               }
               else if (n > 200 && n < 300) {
                   hund3 = count - hund2 - hund1;
               }
               else if (n > 300 && n < 400) {
                   hund4 = count - hund3 - hund2 - hund1;
               }
               else if (n > 400 && n < 500) {
                   hund5 = count - hund4 - hund3 - hund2 - hund1;
               }
               else if (n > 500 && n < 600) {
                   hund6 = count - hund5 - hund4 - hund3 - hund2 - hund1;
               }
               else if (n > 600 && n < 700) {
                   hund7 = count - hund6 - hund5 - hund4 - hund3 - hund2 - hund1;
               }
               else if (n > 700 && n < 800) {
                   hund8 = count - hund7 - hund6 - hund5 - hund4 - hund3 - hund2 - hund1;
               }
               else if (n > 800 && n < 900) {
                   hund9 = count - hund8 - hund7 - hund6 - hund5 - hund4 - hund3 - hund2 - hund1;
               }
               else if (n > 900 && n < 1000) {
                   hund10 = count - hund9 - hund8 - hund7 - hund6 - hund5 - hund4 - hund3 - hund2 - hund1;
               }

              
           }//merscount++;
           n++;
           // System.out.print(arr.length);
           System.out.println(" -------------------------------------------------");
           System.out.println(" Total Primes are: "+count);
           System.out.println("Total Mersennes are: " + merscount);
           System.out.println("Primes per Hundreds are: ");
           System.out.println(hund1 + " , " + hund2 + " , " + hund3 + " , " + hund4 + " , " + hund5 + " , " + hund6 + " , " + hund7 + " , " + hund8 + " , " + hund9 + " , " + hund10);
       }
   }

   public static boolean isPrime(long number) {
       boolean iPrime=true;
       int d;
       for(d=2; d<=Math.sqrt(number);d++){
           if(number%d==0){
               iPrime=false;
               break;
           }
       }
       return iPrime;
   }

   public static boolean isMers (long number){
       boolean iMers=false;
       long mersennePrime = (long)(Math.pow(2, number))-1;
       if((isPrime(number)==isPrime(mersennePrime))){
           iMers=true;
       }
       return iMers;
   }

   public void fileOut(String[] args) {
       File fName2 = new File("outfile.txt");
       try{
            PrintWriter output = new PrintWriter(fName2);
           String[] outArr = new String[100];
       int i;
       for(i=0; i<outArr.length; i++){
           output.println(outArr[i]);

           //check CL parameter usage
           if(args.length !=4) {
               System.out.println("Usage: java ReplaceText sourceFile targetFile oldStr newStr");
               System.exit(1);
           }

           //check if source file exists
           File sourceFile = new File(args[0]);
           if(!sourceFile.exists()){
               System.out.println("Source file " + args[0] + " does not exist");
               System.exit(2);
           }

           //check if target file exists
           File targetFile = new File(args[1]);
           if(targetFile.exists()){
               System.out.println("Target file "+ args[1] + " already exists");
               System.exit(3);
           }

           try {
               //create input and output files
               Scanner input = new Scanner(sourceFile);
               PrintWriter output1 = new PrintWriter(targetFile);

               while(input.hasNext()){
                   String s1 = input.nextLine();
                   String s2 = s1.replaceAll(args[2], args[3]);
                   output1.println(s2);
               }
           }catch (Exception e){System.out.println(e);}
       }
       }catch(Exception e){
            System.out.println(e);
       }
      
   }
}

Sample Output:

-------------------------------------------------                                                                                                               

                                                                                                                                                                

Total Primes are: 168                                                                                                                                           

Total Mersennes are: 9                                                                                                                                          

Primes per Hundreds are:                                                                                                                                        

25 , 21 , 16 , 16 , 17 , 14 , 16 , 14 , 15 , 14  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote