(Prime Numbers) An integer is said to be prime if it\'s divisible by only 1 and
ID: 3546703 • Letter: #
Question
(Prime Numbers) An integer is said to be prime if it's divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. A) Write a function that determines whether a number is prime. B) Use this function is a program that determines and prints all the prime numbers between 2 and 10,000. How many of these numbers do you really have to test before being sure that you've found all the primes? C) Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need only go as high as the square root of n. Why? rewrite the program, and run it both ways. Estimate the performance improvement.Please using the following format: http://ideone.com/kgFegE Because I need to submit this using the code I have learned so far. Thank you. (Prime Numbers) An integer is said to be prime if it's divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. A) Write a function that determines whether a number is prime. B) Use this function is a program that determines and prints all the prime numbers between 2 and 10,000. How many of these numbers do you really have to test before being sure that you've found all the primes? C) Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need only go as high as the square root of n. Why? rewrite the program, and run it both ways. Estimate the performance improvement.
Please using the following format: http://ideone.com/kgFegE Because I need to submit this using the code I have learned so far. Thank you.
Explanation / Answer
Here is another with no cmath.
#include<iostream>
using namespace std;
int isPrime(int n)
{
for (int j=2; j*j<=n; j++)
{
if (n%j==0)
{
return 0;
}
}
return 1;
}
int main() {
int n=10000;
for(int i=2;i<=n;i++)
{
if(isPrime(i)) cout<<i<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.