Need help with a problem

So the whole point of this program is to find the next prime number from a billion.I don't get what the problem is here.

The code is :

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
#include <iostream>
#include <cmath>
using namespace std;

void is_prime(int x);
int main(){
    int numb;

    cout<<"\t\t\tPrime'ing Over a Billion!!!"<<endl<<endl;
    cout<<"Enter a Number : ";
    cin>>numb;
    cout<<endl;

    is_prime(numb)
        ;

}

void is_prime(int x){
    for (x; true; x++){
        for (int i; i < sqrt(x); i++){
            if (x%i != 0){
                cout<<"The next prime number from x is going to be "<<x<<endl;
                break;}
    }
    }
}
What value does i start at?

Furthermore, your algorithm is wrong. Here's a simple example, using x=9, and starting i at 2.:

9%2 does not equal zero, therefore 9 is prime by your algorithm.
Last edited on
i starts off at 0 and hmmmm, didn't think of that.
i starts off at 0

Not in your code. This i:
for (int i; i < sqrt(x); i++)
is some random integer value.1

1. I occasionally hear rumours that some compilers, in (bloated, slow) tricycle mode, will silently initialise values to zero that they really shouldn't. If that's the case, it's not doing you any favours.
Last edited on
Oh seriously?
I thought it went something like int i when started will equal false and since it's false, it'll equal 0.
Never knew that, thanks!
Topic archived. No new replies allowed.