I need help. how can I choose, for a prime number, the prime number that is before and the one that goes after this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I need help. how can I choose, for a prime number, the prime number that is before and the one that goes after this

6th Nov 2018, 4:08 AM
Diana Laura Fuentes
Diana Laura Fuentes - avatar
3 Answers
+ 7
You mean given 17, we need to get 13 and 19?🤔 I couldn't thought of any method except for to find all prime numbers🤔
6th Nov 2018, 4:31 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 7
read this maybe https://en.m.wikipedia.org/wiki/Bertrand%27s_postulate, it can help you to find the prime number succeeding the given number
6th Nov 2018, 9:06 AM
un1xx
un1xx - avatar
+ 4
Using a loop, you'll just have to test all the numbers before (or after) it one by one for primality, starting with the closest one. Whenever you find a prime, you can stop the loop. Also, make sure that your code is not trying to find primes before 2, because there won't be any. It actually doesn't matter whether the number we started with is a prime or not. This is all I can suggest. If you get stuck, please share your attempt, and we'd be happy to help you out.
6th Nov 2018, 4:17 AM
Kishalaya Saha
Kishalaya Saha - avatar