Write a program that figures out how many prime numbers exist within an arbitrar
ID: 3851465 • Letter: W
Question
Write a program that figures out how many prime numbers exist within an arbitrary, random range of numbers.
See next page for more on the "random range" part.
See second-to-next page for tips on how to determine a number's "prime-ness" using simple deduction.
Requirements:
As per usual, be sure to leave some comments (in your code) on how certain complex / unique parts of your program work.
You MUST use a loop in your program, where the code you have inside the body, is the main determining factor of figuring out a number's "prime-ness".
Double check your algorithm's logic to make sure the following numbers are NOT counted as prime:
One, zero, and all negative values
Like always, be sure your output is easy to read and understand. At the bare minimum, make sure the message of what the meaning of the final tally result being printed is clear to the [oblivious] user.
Tips:
Get started by coming up with code that figures out just a single number's "prime-ness" first, then you can make adjustments to your solution afterwards to incorporate the range of values aspect using a [normal] loop.
You can use hardcoded values for the min / max range limits for testing purposes; but keep in mind that your code should work for ANY given range of real numbers. Here are some cases you should try:
0 to 10 ==> has 4 primes: 2, 3, 5, and 7
-10 to 0 ==> has 0 primes
-7 to 7 ==> has 4 primes: 2, 3, 5, and 7
-50 to 50 ==> has 15 primes:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
Think about what major aspects of your code would "fit" within the notion of using "loop logic" as you're [very likely] going to need to use a nested loop.
Set up a Boolean variable to keep track a number's "prime-ness" over time.
How to generate a "random range"
You'll need to have two variables that represent the 'start' and 'end' points of your [imaginary] range. So, in order to initialize them with random values (each time your program runs), you'll need to make use of the following code:
(int) -(50 * Math.random()) //You can replace '50' with
//whatever MIN range value you want
(int) (50 * Math.random()) //You can replace '50' with
//whatever MAX range value you want
How to determine a number's "prime-ness"
There are a number of different ways you can go about solving this part, however the approach I recommend you using is to:
"Assume the number IS prime, until proven FALSE"
How do we prove that a number is not prime? Simply find a divisor from 2 to (number - 1) that divides evenly into the number then we know that the number is no longer prime.
For example, assume we have "number = 9", so the potential divisors are 2 8.
Does 9 / 2 have a remainder, yes, so move on to next divisor.
Does 9 / 3 have a remainder, no, NOW we know '9' is NOT a prime.
Another example, assume we have "number = 5", so the potential divisors are 2 4.
Does 5 / 2 have a remainder, yes, so move on to next divisor.
Does 5 / 3 have a remainder, yes, so move on to next divisor.
Does 5 / 4 have a remainder, yes, so move on to next divisor.
No more divisors to check, this [logically] means that '5' MUST be prime.
Explanation / Answer
PrimesInRange.java
this class has main(). run it!!!
import java.util.ArrayList;
public class PrimesInRange{
//isPrime() checks for prime-ness of a number
//determines a number's "prime-ness"
public static boolean isPrime(int number)
{
//if given number is -ve or 0 or 1, then it will never be prime.
if(number<=1)
return false;
//by default we assume number is prime.
boolean isPrime = true;
//checking for a i which divides number evenly, so that we prove number is not prime
for(int i=2;i<number;i++)
{
if(number%i==0)
{
isPrime=false;
break;
}
}
return isPrime;
}
//generateRandomRange() method randomly generates 2 numbers
public static int[] generateRandomRange()
{
int randomRange[] = new int[2];
randomRange[0] = (int)-(50*Math.random());
randomRange[1] = (int)(50*Math.random());
return randomRange;
}
public static void findPrimesInRange(int start,int end){
//start and end are taken as inclusive to check for primes in the range
//primeStore arraylist stores all prime numbers found within range
ArrayList<Integer> primeStore = new ArrayList<Integer>();
for(int num=start;num<=end;num++)
{
//checking whether num is prime or not.
//if prime, stores in primeStore
if(isPrime(num))
primeStore.add(num);
}
System.out.print("has "+primeStore.size()+" primes");
//if there are some primes in primeStore, prints them
if(primeStore.size()!=0)
{
System.out.print(": ");
for(int prime:primeStore)
System.out.print(prime+", ");
}
}
public static void main(String a[])
{
//System.out.println(generateRandomRange()[0]+" "+generateRandomRange()[1]);
int range[] = generateRandomRange();
findPrimesInRange(range[0],range[1]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.