do anyone know how to print all the prime numbers within a given range | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

do anyone know how to print all the prime numbers within a given range

11th Jul 2017, 7:47 AM
Kartikey Sriwardhan
Kartikey Sriwardhan - avatar
6 ответов
+ 2
Javascript tutorial, but can help to implement such prime number search: https://code.sololearn.com/WPXD8LSoeiIL/?ref=app
11th Jul 2017, 7:55 AM
visph
visph - avatar
+ 2
take two inputs start and end and two for loops as (say i and j) variables. and a flag variable run first for (i.e. i) loop from the start value and increment it till end value.. and initialise the flag value to zero.. then run second for (i.e. j) loop from 2 and increment it..till the j value is less than i. and in the same loop check a condition that whether i is fully divided by j, then increment the value of j.. and atlast check if the value of flag remains zero then print the value of i
11th Jul 2017, 7:59 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
You can start with 6n+/-1 or any other filter and do a sieve of Eratosthenes or segmented sieve of Eratosthenes or any other sieve of your choosing. for an arbitrary range, 6n+/-1 will weed out those divisible by 2 and by 3. then there are primality tests. AKS comes to the mind off handed. There are more. Chances are, they are available as library somewhere. The numbers flagged probably prime by these tests can then be thoroughly tested to eliminate false positives. if you are asking for the code, it is a different question entirely.
11th Jul 2017, 8:06 AM
Venkatesh Pitta
Venkatesh Pitta - avatar
0
for(int i=min; i<max; i++){ bool prime=true; for(int j=2; j<i; j++) if(i%j==0) prime=false; if(prime) cout << i; }
11th Jul 2017, 12:45 PM
Andrés04_ve
Andrés04_ve - avatar
11th Jul 2017, 12:54 PM
Andrés04_ve
Andrés04_ve - avatar
0
thankss... a lot everyone.....
12th Jul 2017, 2:56 PM
Kartikey Sriwardhan
Kartikey Sriwardhan - avatar