0

Question: How to find, for a given integer n, the first prime number that is larger than n?


My own work so far

I've managed to write a program that checks whether or not a given integer is a prime or not:

#include <iostream>
#include <cmath>

using namespace std;

bool is_prime (int n)
{
    int i;
    double square_root_n = sqrt(n) ;
    for (i = 2; i <= square_root_n ; i++)
    {
        if (n % i == 0){
            return false;
            break;
        }
    }
    return true;
}

int main(int argc, char** argv)
{
    int i;
    while (true)
    {
        cout << "Input the number and press ENTER: \n";
        cout << "To exit input 0 and press ENTER: \n";
        cin >> i;
        if (i == 0)
        {
            break;
        }
        if (is_prime(i))
        cout << i << " is prime" << endl;
        else
        cout << i << " isn't prime'" << endl;
    }
    return 0;
}

I'm struggling, however, on how to proceed on from this point.

dfrib
  • 70,367
  • 12
  • 127
  • 192
Uros.M
  • 11
  • 4
  • 2
    What is your problem? What happens when you compile and run your code? Do you get any errors? If so, what are they? If not, what is the output you get when you run your program? How does this differ from what you want? – Code-Apprentice Aug 06 '16 at 21:25
  • You should add 0.5 to you double value and truncate to int to avoid problems. Otherwise you code looks fine. Can you be more specific about your problem? – Jean-François Fabre Aug 06 '16 at 21:27
  • seriously, I just went ahead and removed all the non-info and redundancy from your question. Keep things short and precise. – Marcus Müller Aug 06 '16 at 21:28
  • You can find online tools that will help you format the code. Look for "online C formatter" or similar terms. – Elazar Aug 06 '16 at 21:29
  • if you see this, why don't you just translate the strings to English? It makes it easier for others to help you. Also, I formatted your code – every code editor should have functionality to do this for you. Use that! – Marcus Müller Aug 06 '16 at 21:29
  • 1
    Variable names that are non-English are a bad idea, because they make it harder for us to help you. Your code is part of your question. Translate it as good as you can; your English is excellent, so this shouldn't be a problem for you! – Marcus Müller Aug 06 '16 at 21:31
  • "give me tips what I should add / change" is far too broad. Ask a clear question, so that we can answer. @Code-Apprentice explained all the things we need to know from you. – Marcus Müller Aug 06 '16 at 21:31
  • This code is correct,I am just asking for what i should add so it can cout next prime number – Uros.M Aug 06 '16 at 21:33
  • 3
    @MarcusMüller The OP registered for SO today, and describes himself as a high school student, so imho, possibly a more friendly, welcoming and official-SO-guidelines-links explanation of the way of SO questioning could be more appropriate. – dfrib Aug 06 '16 at 21:33
  • @MarcusMüller Personally, I'm okay with non-English variable names. For simple, homework-assignment-type programs, it is usually not difficult to figure out what is going on. The bigger problem with this question is the lack of error messages and other example output. – Code-Apprentice Aug 06 '16 at 21:33
  • Note that `return false` returns immediately, so the `break` will never execute. – Elazar Aug 06 '16 at 21:33
  • @Uros.M What do you mean by "next prime number"? Can you explain, in English, the steps needed to do this? – Code-Apprentice Aug 06 '16 at 21:35
  • If your code can check if a given number is prime, you can check the numbers one by one until the code returns true; that's the next prime. – Elazar Aug 06 '16 at 21:39
  • Is it clear enoguh now? – Uros.M Aug 06 '16 at 21:40
  • See http://stackoverflow.com/a/5694432/576911 for a very thorough treatment of this question. – Howard Hinnant Aug 06 '16 at 21:49
  • @Elazar It would be nice if you could give me that in code,please? :) – Uros.M Aug 06 '16 at 21:49
  • @Uros.M first of all, welcome to Stack Overflow (SO)! Before you proceed with your coding and the solution of this question of yours, have a look at the SO pages [How do I write a good question](http://stackoverflow.com/help/how-to-ask) and [How to create a minimal, verifiable and complete example](http://stackoverflow.com/help/mcve). By reading these guidelines, you will learn to how to ask questions that are more likely to yield good answers for you. Also, asking for code written for you will most likely not yield good feedback, it's better to ask for hints! (Also for your own learning). – dfrib Aug 06 '16 at 21:52
  • 1
    @Uros.M Also note that I took the freedom to attempt to clean up your questions such that it tries to, somewhat, to follow these guidelines (possibly still a bit too broad). Along the sense of posting a _minimal_ example, I remove the declaration of the `is_prime(...)` function and simply move the function definition to proceed `main(...)`. The more minimal example (without loosing clarity), the better. Finally, have a look at [the thread linked to by Howard Hinnant above](http://stackoverflow.com/questions/4475996), which treats this very subject. Good luck with your StackOverflowing! – dfrib Aug 06 '16 at 21:55

2 Answers2

2

You have a function is_prime(n), and a number n, and you want to return the smallest number q such that is_prime(q)==true and n <= q:

int q = n;
while (!is_prime(q)) {
    q++;
}
// here you can be sure that
// 1. q is prime
// 2. q >= n       -- unless there was an overflow

If you want to be a bit more efficient, you can check explicitly for the even case, and the increment by 2 each time.

It's a concrete example of a general theme: if you have a test function and a method for generating elements, you can generate the elements that pass the test:

x = initial_value
while (something) {
    if (test(x)) {
        // found!
        // If you only want the first such x, you can break
        break;
    }
    x = generate(x)
}

(note that this is not a valid C++ code, it's pseudocode)

Elazar
  • 20,415
  • 4
  • 46
  • 67
0
int i;
**int k_koren_od_n = (int)(sqrt(n) + 0.5)**
for (i = 2; i <= k_koren_od_n ; i++){

To get around casting issues, you might want to add this fix.

Sandeep
  • 178
  • 9