954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Prime Finder

I'm trying to make a program where you enter the number you want to find primes up to which then lists all the primes up to that number... But I enter 100 then it just sits there... Here's my code so far:

#include <iostream>
#include <cmath>
int prime(int);

int main()
{
using namespace std;
int upto;
int current = 3;
cout << "Find primes up to: ";
cin >> upto;
while(current < upto){
        cout << prime(current) << endl;
}
system("pause");
return 0;
}

int prime(int p)
{
int test = p - 1;
while(test > -1){
if(test == 0){
        return p;
}
else if(test % p ==  0){
     return 0;
}
else
    test - 1;
}
}
Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

I think the problems in the way i put the formula for primes into code.

Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

You never change the value of your variable "current," so your program can never terminate.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

thanks but i might still have issues. ill post new code if that is the case.

Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

my new code looks like this

#include <iostream>
#include <cmath>
int prime(int);

int main()
{
using namespace std;
int upto;
int current = 3;
cout << "Find primes up to: ";
cin >> upto;
while(current < upto){
        cout << prime(current) << endl;
        current += 1;
}
system("pause");
return 0;
}

int prime(int p)
{
int test = p - 1;
while(test > -1){
if(test == 0){
        return p;
}
else if(test % p ==  0){
     return 0;
}
else
    test - 1;
}
}

when i run it and enter the value it doesnt output anything or end ?

Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

That's because you have the same problem in your prime function: You never change the value of the variable test, so the program never terminates.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

i believe i do
else
test - 1;
}

Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

sorry forgot to do

else
test -= 1;
Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

now it "works" but it just outputs all the numbers 3+ to the number you entered. again heres my code

#include <iostream>
#include <cmath>
int prime(int);

int main()
{
using namespace std;
int upto;
int current = 3;
cout << "Find primes up to: ";
cin >> upto;
while(current < upto){
        cout << prime(current) << endl;
        current += 1;
}
system("pause");
return 0;
}

int prime(int p)
{
int test = p - 1;
while(test > -1){
if(test == 0){
        return p;
}
else if(test % p ==  0){
     return 0;
}
else
    test -= 1;
}
}
Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

You have (at least) two problems.

The first one is that the expression test % p in line 27 should be p & test.

The second is more subtle. Look at the loop in lines 23-32. You will see that the only way that your prime function can ever return anything but 0 is if the variable "test" reaches 0.

However, before it reaches 0, it has to reach 1--and when it does, line 27 will compute p % test (after you've fixed it) with test equal to 1, which will always be zero. So the function will always return zero.

I'm not going to tell you how to solve this problem, because I've already done enough of your homework for you.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

thanks for all the help you dont need to do anything else. happy posting!
You have (at least) two problems.

The first one is that the expression test % p in line 27 should be p & test.

The second is more subtle. Look at the loop in lines 23-32. You will see that the only way that your prime function can ever return anything but 0 is if the variable "test" reaches 0.

However, before it reaches 0, it has to reach 1--and when it does, line 27 will compute p % test (after you've fixed it) with test equal to 1, which will always be zero. So the function will always return zero.

I'm not going to tell you how to solve this problem, because I've already done enough of your homework for you.

Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

FIXED:

#include <iostream>
#include <cmath>
int prime(int);

int main()
{
  using namespace std;
  int upto;
  int current = 3;
  cout << "Find primes up to: ";
  cin >> upto;
  cout << "upto = " << upto << endl;
  while(current < upto){
  if(prime(current) != 0){
    cout << prime(current) << endl;
    }
    current += 1;
  }
  system("pause");
  return 0;
}

int prime(int p)
{
  int test = p - 1;
  while( test > 1 ){
    if( p % test ==  0 ){
      return 0;
    }
    else
      test -= 1;
  }
  return p;
}
Progr4mmer
Junior Poster
113 posts since Nov 2009
Reputation Points: 11
Solved Threads: 10
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: