ANYONE

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.




HOW CAN I FIX THIS CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
 
#include <cmath>
 
int primeNumber(int);
 
int main()
{
   for(int x = 1; x <= 100000; x += 2)
   {
      if( x == primeNumber(x))
         cout << x << endl;
   }
 
   return 0;
}
int primeNumber(int x)
{
   int prime = 0;
 
   for(int j = 2; j < sqrt(x); j++)
   {
      if(x % j == 0)
         prime++;
 
   }
   if(prime == 0)
      return x;
}
It's not wrong code, just very ineficient. You don't want to calculate the square root more than once, and you don't want to continue iterating when you find a case that fails the prime test.
1
2
3
4
5
6
7
8
9
10
11
int primeNumber(int x)
{
   int prime = 0;
   int squarert = sqrt(x);
   for(int j = 2; j < squarert; j++)
   {
      if(x % j == 0)
         return 0;
   }
   return x;
}
Thank you so much, I'm going to try that right now.
This part of the code is wrong , like the sqrt(x); I have a red line under it and I don't know to fix it .

int squarert = sqrt(x);
2 possibilities:
1) Add #include <cmath> at the top of your file
2) Change sqrt(x) to sqrt((double)x)

If you try to compile your code, it should give you an error which is more descriptive than a red line... That should help you to figure out what is wrong and/or which of the options above to use. If these steps don't work, give us that error message.
Last edited on
Stewbond's suggestion #2 should fix your problem.

Also, in your original code, what value is returned at line 32 if prime is not equal to zero?
Last edited on
Cool . Going to check it right now . . .
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



This is what I have now , but the code is running nonstop when I debug it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
 
#include <cmath>
 
int primeNumber(int);
 
int main()
{
   for(int x = 1; x <= 100000; x += 2)
   {
      if( x == primeNumber(x))
         cout << x << endl;
   }

   return 0;
}
int primeNumber(int x)
{
	int prime = 0;

   for(int j = 2; j <sqrt((double)x); j++)
   {
      if(x % j == 0)
         prime++;
   }
   if(prime == 0)
      return x;
}
What do you mean by "nonstop"?

It looks like you totally ignored Stewbond's suggestion #1 and AbstractionAnon's hint.
Last edited on
The Black screen , runs til 10000 then disappears .

sorry to bother you guys , very new here to c++
The Black screen , runs til 10000 then disappears .

http://www.cplusplus.com/forum/beginner/1988/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
 
#include <cmath>
 
int primeNumber(int);
 
int main()
{
   for(int x = 1; x <= 100000; x += 2)
   {
      if( x == primeNumber(x))
         cout << x << endl;
   }
  
   return 0;
}
int primeNumber(int x)
{
	int prime = 0;

   for(int j = 2; j <sqrt((double)x); j++)
   {
      if(x % j == 0)
         prime++;
   }
   if(prime == 1)
      return x;
}
Do I need system ("pause"); before the line 18 ?
Not the best solution, but yes that will stop the console from closing.
Topic archived. No new replies allowed.