I have most of this code done, but whenever I run it, It gives me mostly prime numbers with some non-prime mixed in (like 15 and 21). Can anyone point me in the right direction?
here is the code so far:
/*
File: isprime.cpp
Created by: Ricardo Renta
Creation Date: 11/8/11
Synopsis:
This program reads in a minimum and maximum integer greater than 1
and returns all primes between the minimum and maximum.
*/
#include <iostream>
#include <cmath>
using namespace std;
// FUNCTION PROTOTYPE FOR read_range
void read_range( int& imin, int& imax);
// FUNCTION PROTOTYPE FOR is_prime
bool is_prime (int k); //boolean prototype for true and false
int main()
{
int imin(0), imax(0);
// Read in range
read_range(imin, imax);
// Print prime numbers
cout << "Primes:";
for (int k = imin; k <= imax; k++) {
if (is_prime(k))
{
cout << " " << k;
}
}
cout << endl;
return 0;
}
// DEFINE FUNCTION read_range() HERE:
void read_range(int& imin, int& imax)
{
cout << "Enter a minimum and maximum value:";
cin >> imin >> imax;
while (imin < 2 || imax < 2 && imin > imax)
{ cout << "The minimum and maximum values must be at least 2. Please try again."<< endl;
cin >> imin >> imax;
}
}
// DEFINE FUNCTION is_prime() HERE:
bool is_prime(int k)
{
for (int i=2; i<=(k-1); i++)
{
if (k%i==0 && k!=i)
{return false;}
bool is_prime(int k)
{
for (int i=2; i<=(k-1); i++)
{
if (k%i==0 && k!=i)
{returnfalse;}
else
{returntrue;}
}
}
is wrong. During the first iteration in the loop it tests if k is even. If it is even then the function returns false, otherwise it returns true.
You may try to modify its text as follows:
1 2 3 4 5 6 7 8 9
bool is_prime(int k)
{
for (int i=2; i < k / 2; i++) //you don't need to check all numbers, k/2 is enough or even sqrt(k)
if (k%i==0) // k is never equals to i
returnfalse;
returntrue;
}
Hmmmm....I tried the sqrt(k) before, and it didn't work. I tried changing it to what you suggested, and when I put the lower and upper bounds as 3 and 15, respectively, it gave me this output:
#include<iostream>
usingnamespace std;
bool is_prime(int k){
for (int i=2; i < k / 2; i++) //you don't need to check all numbers, k/2 is enough or even sqrt(k)
if (k%i==0) // k is never equals to i
returnfalse;
returntrue;
}
int main(){
for (int i = 2; i < 20; i++)
if (is_prime(i)) cout << i << endl;
}
produces the following output:
2
3
4
5
7
11
13
17
19
Neither 9 nor 15 is included. Only 4 is extra.
So, little change from for (int i=2; i < k / 2; i++) to for (int i=2; i <= k / 2; i++)
1 2 3 4 5 6 7 8
bool is_prime(int k)
{
for (int i=2; i <= k / 2; i++)//you don't need to check all numbers, k/2 is enough or even sqrt(k)
if (k%i==0) // k is never equals to i
returnfalse;
returntrue;
}
bool is_prime(int k)
{
for (int i=2; i<=(k-1); i++)
{
if (k%i==0 && k!=i)
returnfalse;
}
returntrue;
}
also work.
In your version of code you will return either true or false just after the first iteration of the loop (i = 2, if k is odd then condition (k%i==0 && k!=i) doesn't hold and you go to else block and returns true.
After you return the function terminates and its all: the result of function is true for every odd input.
Pay attention that in my version returntrue; executes at the end of the function body: result is true only if every (k%i==0 && k!=i) fails (i.e. we never executed returnfalse;).