Note that there are some explanatory texts on larger screens.

plurals
  1. POPrime numbers calculation with python
    primarykey
    data
    text
    <p>I know, there are a lot of questions about prime numbers already, but I don't ask for a code. I just want to know, what is wrong with the mine (I hope comments would help you understand, what I'm doing):</p> <pre><code>from math import ceil def isPrimeNumber (n, out='short'): #Checks, whether n is a prime number answer = 'Yes' for p in range(2, int(ceil(n**0.5))+1): #Checks all numbers lower than #SQRT(n), if they are divisors of n if n%p == 0: #If p is a divisor, n isn't prime answer = 'No' if out == 'verbose': print 'Least divisor is', p return False break if answer == 'Yes': #If there isn't a p, that is a divisor, n is prime if out == 'verbose': print 'No divisors except for 1 and', str(n)+'!' return True def primeNumbers (start = 1, stop = 1000, numbers = 0): N = stop if numbers == 0: #Calculates all prime numbers in N numbers in a row #(N=1000 -&gt; calculates all prime numbers in 1000 numbers, #by default from 1 to 997) primes = [] for i in range(start, N+1): if isPrimeNumber(i) == True: primes.append(i) elif numbers == 1: #Calculates N prime numbers in a row #(N=1000 -&gt; calculates 1000 prime numbers) primes = [start] i = len(primes) j = 1 while i &lt;= N: #Stops, when we get N prime numbers - doesn't work! n = max(primes) + 1 while j != 'stop': if isPrimeNumber(n, out='short') == True: primes.append(n) i = i + 1 j = 'stop' #Stops nested cycle, when reached #the first prime number else: n = n + 1 else: print 'Wrong input! 3rd number in function call must be either 0 or 1' return primes </code></pre> <p>Function isPrimeNumber() works fine. Function primeNumbers works fine as well, when numbers=0. But if numbers=1, then, as it seems, one of the cycles never stops, I can't understan why...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload