Help with primes program

Pages: 12
Hi,

So my function works flawlessly up to where it is supposed to print the prime numbers. No matter what numbers i put in, all it outputs is "Primes " nothing else. Im not sure where in this program its not working so any help is appreciated.


/*
File: isprime.cpp
Created by: James Selhorst
Creation Date: 10/22/12
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);

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
int imin(0), imax(0);
int prime(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& imax, int& imin)
{
cout<<"Enter minimum and maximum:";
cin>>imin>>imax;
while(imin>imax)
{
cout<<"Error, Minimum must be less then maximum"<<endl;
cout<<"Enter minimum and maximum:";
cin>> imin>>imax;
}
while((imin<2 || imax<2))
{
cout<<"Error, Minimum and maximum must be at least 2"<<endl;
cout<<"Enter minimum and maximum:";
cin>> imin>>imax;
}
}


// DEFINE FUNCTION is_prime() HERE:
bool is_prime(int& k)
{
if(k%(k-1)!=0)
true;
else
false;
}


Giving free E hugs for help .
check the values of imin,imax and the returned value of is_prime in

cout << "Primes:";
for (int k = imin; k <= imax; k++)
{
if (is_prime(k))
...
That doesnt really help me honestly. the imin and imax are input in (read_range)
i doubt cout << " " << k; even gets called. set a breakpoint and see if it stops
Does anyone else have a solution to why this program will not display any of the primes, only prints out Primes: blank space.
Please need HELP!!!!!!
Ive been working on this program for hours upon hours and this is the last part to finally finish and turn my assignement in super late so please i need someone to point me in the right direction or simply tell me what to change to make it work, im begging at this point.
BTW, in future always post your code with code tags - the <> button on the right..

In the IsPrime function you have:

if(k%(k-1)!=0)

To understand this, grab a pencil & paper, and evaluate the formula for consecutive numbers. It doesn't matter where you start.

7 mod 6 =
8 mod 7 =
9 mod 8 =

So what are the results of this for your if condition?

HTH

Last edited on
if i understand it correctly the if statement will always show true, because no matter what number you put in the mod isnt 0
so i understand that i should make a for loop that checks every number from 2 to (number -1) and if none of them = 0 its true and a prime so i created

{
for(int i=2; i<=(k-1); i++)
{
if (k%i !=0)
true;
}
}

but still only get a blank answer for primes.
and if none of them = 0 its true and a prime so i created


What you have said is right, but it doesn't match your code fragment.

You have a loop to check all the numbers, but there is a small thing missing from the logic.

When you get this working, it will be fine up to a limit, because it is a very exhaustive method. Prime numbers become very sparse, so a more efficient method has to be used.

There are lots of examples of algorithms for this on the net.
can you tell me what im doing wrong, ive been at this a long time and only missing a small part...
So you need to count the numbers where(k%i !=0) is true.
Can you give me more advice then it needs a counter. I understand if say your min is 9 max is 12 it will start with 9 and pull it into is_prime

i=2, 9%2 !=0
i=3, 9%3=0
i=4 9%4 !-0

this would show false more then once because at points it =0, however when it hits say 11 ,

from 1-10, 11%(1-10) !=0, so it would come out true every time.
should i make a counter saying only it comes out true 1 time then the whole thing is true?
So it came to me what if i did this
{
int count=0;
for(int i=2; i<=sqrt(k); i++)
{
if (k%i !=0)
count++;

}
if (count==(k-1))
true;
else
false;

}
but low and behold it didnt work either
Need help please, I think I'm almost there and after 8 hours on this I just need the final bit, so please !!!
http://www.cplusplus.com/forum/general/82230/#msg446205


Here is a good solution by jumper007, there is a really fancy one he posted further down.
I'm not sure where this code fragment fits into the big picture, is it a function, or a part of something else?
Please use code tags, like this
1
2
3
4
5
6
7
8
9
10
11
12
13
{
    int count=0;
    for(int i=2; i<=sqrt(k); i++)
    {
        if (k%i !=0)
            count++;
    }
    
    if (count==(k-1))
        true;
    else
        false;
}


Lines 8 through 12 don't do anything useful. In particular, lines 10 and 12 are pretty much useless. You need to either assign true/false to some variable which you will later test, or if this is meant to be a function, then maybe it needs to return some value?

As for counting the number of times the remainder is non-zero, that's more than is necessary.

Test the opposite condition instead. If the remainder is equal to zero, the number is not prime. At that point you can exit from the for-loop, as any subsequent tests are irrelevant.



#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);

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
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& imax, int& imin)
{
cout<<"Enter minimum and maximum:";
cin>>imin>>imax;
while(imin>imax)
{
cout<<"Error, Minimum must be less then maximum"<<endl;
cout<<"Enter minimum and maximum:";
cin>> imin>>imax;
}
while((imin<2 || imax<2))
{
cout<<"Error, Minimum and maximum must be at least 2"<<endl;
cout<<"Enter minimum and maximum:";
cin>> imin>>imax;
}
}


// DEFINE FUNCTION is_prime() HERE:
bool is_prime(int& k)
{
int count=0;
for(int i=2; i<=(k-1); i++)
{
if (k%i !=0)
count++;

}
if (count=(k-2))
true;


}


this is my full code chervil , i cant figure it out, i gave code tags ext, can you please just tell me how to fix this , NO one will just give me the freaking answer and im so tired of looking at this and getting encrypted hints to "figure" it out.
@TheIdeasMan

I cant even begin to understand jumper007's code, im in a beginners class using the very basic of coding like cout ext. So when looking at his code and seeing vix and bitset library , the whole thing is useless to me. Can you tell me how to fix the code i have now without encrypted hints, for example, if you see the exact problem... tell me
Pages: 12