Will some one please modify this code so that it creates an arrayand stores all
ID: 3616282 • Letter: W
Question
Will some one please modify this code so that it creates an arrayand stores all the prime numbers in it as it finds them. And also,instead of having (n%i==0) in the isPrime method, have n % by theprime numbers in the array that are less than the square root of n.Thanks very very very much.public class Jan19b
{
public static boolean isPrime(int n)
{
int root = (int) Math.sqrt(n);
for (int i=2; i<=root; i++) {
if ((n%i) == 0) {
return false;
}
}
return true;
}
public static void main(String args[])
{
int c=0;
long start, end, mstime;
final int LIMIT=100; // 1M took ~800 secs.
start = System.currentTimeMillis();
for (int i=2; i<LIMIT; i++) {
if (isPrime(i)) {
System.out.println(i);
c++;
}
}
end = System.currentTimeMillis();
mstime = end - start;
System.out.println("#primes = " + c + ", mstime = " + mstime);
}
}
Explanation / Answer
please rate - thanks public class untitled { static intc=0; static int primes[] = newint[500]; public static boolean isPrime(int n) { int root = (int)Math.sqrt(n); for (int i=2;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.