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

These all parts are related with each other please answer all of them. Write a f

ID: 3819423 • Letter: T

Question

These all parts are related with each other please answer all of them.

Write a function named prime() that determine whether or not a given number n (greater than one) is prime. The algorithm: If n is even then n is not a prime number except n = 2 For all odd numbers less or equal to the squareroot of n, if any of them divide n, then n is not prime Write a program that: Asks the user to enter an integer. Saves the entry as var. Calls the function prime() on the entry. Displays whether or not the entry is prime Write a function named eratos() that find all the prime number less than a given number, its argument n Write a program that: Asks the user to enter an integer. Saves the entry as bound. Passes bound onto eratos(). Displays all the prime numbers less or equal to bound

Explanation / Answer

import java.util.Arrays;

import java.util.Scanner;

public class Chegg {

   // Part 3

   static boolean isPrime(int n){

       if (n % 2 == 0)

           return false;

       for (int i=3; i*i <= n; i+=2){

           if(n % i == 0)

               return false;

       }

       return true;

   }

   static boolean[] primes;

   // Part 5

   static void eratos(int n) {

       primes = new boolean[n];

      Arrays.fill(primes,true);

      primes[0]=primes[1]=false;

      for (int i=2;i<primes.length;i++) {

      if(primes[i]) {

      for (int j=2;i*j<primes.length;j++) {

      primes[i*j]=false;

      }

      }

      }

   }

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       String input = scan.next();

       // Part 4

       boolean prime = isPrime(Integer.parseInt(input));

       if(prime){

           System.out.println(input +" is prime");

       }else{

           System.out.println(input +" is not prime");

       }

      

       // Part 6

       String input2 = scan.next();

       eratos(Integer.parseInt(input2));

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

           if(primes[i]){

               System.out.println(i);

           }

       }

   }

}

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