Question: I m providing starting code that you will need to use for this. We want to find out all the prime numbers between 1 and
Im providing starting code that you will need to use for this. We want to find out all the prime numbers between and some user entered value.
Algorithm explanation: we will create a boolean array that size and make all values initially true. Then we will switch all values divisible by to false. Then we will repeat for We will continue until we have checked them all. You need to think hard about what it means to "have checked them all". Be as efficient as you can.
NOTE: you might think that n is the upper limit for which you must test to see whether a number n is prime, but you need only go as high as the square root of n Think about it
import java.util.Scanner;
We want to find out all the prime numbers between and some user entered value
Algorithm explanation : we will create a boolean array that size and make all values
initially true. Then we will switch all values divisible by to false. Then we will
repeat for We will continue until we have checked them all. You need to think hard
about what it means to "have checked them all". Be as efficient as you can
public class Primes
public static void mainString args
Scanner scanner new ScannerSystemin;
System.out.printlnThis program will create all the prime numbers for you: ;
System.out.printlnWhat is your maximum number?";
int max scanner.nextInt;
scanner.close;
create a boolean array that is one size larger than the max variable
ex if I want to check primes for I want to set up a boolean array with index
we will ignore the zeroth location
loop through the array that you made and make each value initally equal to true
utilize a for loop to switch all non prime locations to false.
I used a for loop inside of an if that was inside of my main for loop
so starting with I want to switch etc. up to less than max
then do the same for Switch which is already switched up to less than max
etc.
see if you can optimize your algorithm.
call your printPrime method see below to print the prime numbers
write a method printPrimeint n boolean nums that passes in the max number and the
boolean array. Have it print only the prime numbers, all on one line. See sample output
SAMPLE OUTPUT:
This program will create all the prime numbers for you:
What is your maximum number?
Prime numbers smaller than :
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
