Hi i'm making a program that prints the all the prime numbers there are in a limit that the user enters. So if the user enters the limit as 10 the prog should print 1,2,3,5,7. I made the prog using the bool data type however i'm not supossed to use it
Can anyone tell me how to make the prog without using bool? Heres what i did
#include using namespace std;
void find_primes (bool p[], long n);
bool is_prime (long i);
void print_primes (bool p[], long n);
int main () {
const long MAXPRIME = 1000000; // 10^6
bool prime[MAXPRIME + 1];
long limit;
// start and finish times
// get limit from user
cout << "This program finds primes using a naive method.
";
cout << "It will search for primes from 1 up to a user-specified limit.
";
cout << "Limit must be less than or equal to " << MAXPRIME << endl;
cout << "Enter limit: ";
cin >> limit;
if (limit <= MAXPRIME) {
find_primes(prime, limit);
print_primes(prime, limit);
return 0;
}
else {
cerr << "Sorry, that number is out of range
";
return 1;
}
}
void find_primes (bool p[], long n) {
for (long i = 1; i <= n; i++)
p[i] = is_prime(i);
}
bool is_prime (long i) {
for (long d = 2; d * d <= i; d++)
if (i % d == 0)
return false;
return true;
}
void print_primes (bool p[], long n) {
for (long i = 1; i <= n; i++)
if (p[i])
// i is prime, print it
cout << i << endl;
}
Comments
:
: #include
:
: using namespace std;
:
: void find_primes (bool p[], long n);
: bool is_prime (long i);
: void print_primes (bool p[], long n);
:
: int main () {
: const long MAXPRIME = 1000000; // 10^6
: bool prime[MAXPRIME + 1];
: long limit;
: // start and finish times
:
: // get limit from user
: cout << "This program finds primes using a naive method.
";
interesting....a naive method
: cout << "It will search for primes from 1 up to a user-specified limit.
";
: cout << "Limit must be less than or equal to " << MAXPRIME << endl;
: cout << "Enter limit: ";
: cin >> limit;
:
: if (limit <= MAXPRIME) {
: find_primes(prime, limit);
: print_primes(prime, limit);
:
: return 0;
: }
: else {
: cerr << "Sorry, that number is out of range
";
: return 1;
: }
: }
:
: void find_primes (bool p[], long n) {
: for (long i = 1; i <= n; i++)
: p[i] = is_prime(i);
: }
:
: bool is_prime (long i) {
: for (long d = 2; d * d <= i; d++)
: if (i % d == 0)
: return false;
: return true;
: }
:
: void print_primes (bool p[], long n) {
: for (long i = 1; i <= n; i++)
: if (p[i])
: // i is prime, print it
: cout << i << endl;
: }
:
:
:
:
-Mel
: Hi i'm making a program that prints the all the prime numbers there are in a limit that the user enters. So if the user enters the limit as 10 the prog should print 1,2,3,5,7. I made the prog using the bool data type however i'm not supossed to use it Can anyone tell me how to make the prog without using bool? Heres what i did
:
: #include
:
: using namespace std;
:
: void find_primes (bool p[], long n);
: bool is_prime (long i);
: void print_primes (bool p[], long n);
:
: int main () {
: const long MAXPRIME = 1000000; // 10^6
: bool prime[MAXPRIME + 1];
: long limit;
: // start and finish times
:
: // get limit from user
: cout << "This program finds primes using a naive method.
";
: cout << "It will search for primes from 1 up to a user-specified limit.
";
: cout << "Limit must be less than or equal to " << MAXPRIME << endl;
: cout << "Enter limit: ";
: cin >> limit;
:
: if (limit <= MAXPRIME) {
: find_primes(prime, limit);
: print_primes(prime, limit);
:
: return 0;
: }
: else {
: cerr << "Sorry, that number is out of range
";
: return 1;
: }
: }
:
: void find_primes (bool p[], long n) {
: for (long i = 1; i <= n; i++)
: p[i] = is_prime(i);
: }
:
: bool is_prime (long i) {
: for (long d = 2; d * d <= i; d++)
: if (i % d == 0)
: return false;
: return true;
: }
:
: void print_primes (bool p[], long n) {
: for (long i = 1; i <= n; i++)
: if (p[i])
: // i is prime, print it
: cout << i << endl;
: }
:
:
:
: