A positive integer is prime if it\'s divisible by only 1 and itself. number 1 by
ID: 3635000 • Letter: A
Question
A positive integer is prime if it's divisible by only 1 and itself. number 1 by definition is not prime.
Use this method in an application that determines & display all the prime numbers less than 10,000. how many #s up to 10,000 do you have to test to ensure that you've found all the primes?
Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is n is prime, but you need only go as high as the square root of n. Rewrite the program and run it both ways.
Explanation / Answer
Please Rate:Thanks
public class Prime10000 {
public static void main(String[] args){
for(int i=1;i<=10000;i++){
if(isPrime(i)==true)
System.out.print(i+" ");
}
}
public static boolean isPrime(int n){
int count=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}
}
----------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.