Saturday, 15 September 2012

calculator - Calculating the sum of primes in Python - what purpose does this line of code serve? -



calculator - Calculating the sum of primes in Python - what purpose does this line of code serve? -

i have short function calculate sum of prime numbers limit. here total code context:

def primes_sum(limit): limitn = limit+1 not_prime = [false] * limitn primes = [] in range(2, limitn): if not_prime[i]: go on f in xrange(i*2, limitn, i): not_prime[f] = true primes.append(i) homecoming sum(primes)

most of makes sense me, don't understand part:

not_prime = [false] * limitn

what purpose of this? how work , how find out whether or not number prime? i've tried looking up, can't find answer.

thanks in advance.

you creating list of length limitn it's element initialized false.

* repetition operator. when applied on list integer value n right operand, repeats list n times.

you can utilize on strings also:

>>> [false] * 2 [false, false] >>> "rohit" * 2 rohitrohit

python calculator

No comments:

Post a Comment