0

How to Detect prime numbers?

between 2-1000

15th Sep 2018, 5:04 PM
Debjit Mutsuddi
10 RĂ©ponses
+ 2
To detect if a number n is prime, check if any number from 2 to square root of n divides n. To find all primes in a range (2 - 1000), you can use sieve of erathosthenes, described here https://www.sololearn.com/learn/969/?ref=app
15th Sep 2018, 5:08 PM
michal
+ 2
oh, didn't see that one, sloppy
15th Sep 2018, 8:32 PM
davy hermans
davy hermans - avatar
+ 1
this code contains a function that detects primes. you can use it on every number in the range. https://code.sololearn.com/c7I7yNzPppBS/?ref=app
15th Sep 2018, 5:24 PM
davy hermans
davy hermans - avatar
+ 1
Debjit Mutsuddi You can find some codes in the comments of the task linked by michal
15th Sep 2018, 6:52 PM
Matthias
Matthias - avatar
+ 1
davy hermans Your code gives wrong answer for 2. It is not actuallynecessary to iterate to n/2, square root of n suffices
15th Sep 2018, 7:46 PM
michal
+ 1
Debjit Mutsuddi , thanks. Fixed it.
15th Sep 2018, 7:51 PM
davy hermans
davy hermans - avatar
0
can't find the code
15th Sep 2018, 5:11 PM
Debjit Mutsuddi
0
but thanx anyway
15th Sep 2018, 5:11 PM
Debjit Mutsuddi
0
thanx
15th Sep 2018, 7:16 PM
Debjit Mutsuddi
0
davy hermans Still doesn't work. Put return true outside of the loop
15th Sep 2018, 8:21 PM
michal