5 Years
Discussion Span
Last Post by Lucaci Andrew

I'm a bit confused on what you want, do you want to output a list of prime numbers whose range is upto the input or check if the input is a prime number or something else entirely?


Ok, so you need, if, for example, 5 is inputed, to show you 7, and if 7 is inputed to shouw you 11, and so on and so forth.
Well, we'll need a proof that you tried to solve your problem, so that we know where you're stuck. Post what you've done so far:).



i'm not stuck enywhere, just need to know formula or smth,i'm using just prime numbers identification
for (i = 2; i<=sqrt(n); i++)}
if (n % i == 0); // If i divides n evenly,*

...and ftw with this rich text box- why it marks all the sentence i wrote... and can't sometimes edit in here?


well you need to use a loop and go from the next odd number greater than what you entered untill you find a number that is prime.

bool prime = false;
    // check if number is prime
    // if number is prime set prime to true
    // increment number to check

Here's something I found on Google:

bool checkPrime(int num){
    if(num<=1) return false;
    if(num==2) return true;
    if(num%2==0) return false;
    for(int i=3; i<=sqrt(num*1.0); i+=2)
        if(num%i==0) return false;
    return true;

Also, if you want to get the next prime number, you could do it like this:

while(++nr && !checkPrime(nr));

And after the loop ends, you'll have your next prime number.

Edited by Lucaci Andrew

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.