1

I am a beginner in python and writing a program to calculate the primes numbers till given N. here is my code. what I see is that my list called numbers is changed after each iteration through for-loop. however, I see the list from which I am iterating over still contains reference to old values while this list is changed with for loop. kindly look at the output below.

def getprimes(n):
         numbers = [ i for i in range (2,n+1)]
         primes=[]
         print "numbers in the beginning",numbers

         for i in numbers:
                print i
                primes.append(i)
                print "primes",primes
                print "numbers",numbers
                numbers=[k for k in numbers if (k%i!=0)]

         print "numbers at end",numbers
         return primes


print getprimes(10)

my output is below:

numbers in the beginning [2, 3, 4, 5, 6, 7, 8, 9, 10]
2
primes [2]
numbers [2, 3, 4, 5, 6, 7, 8, 9, 10]
3
primes [2, 3]
numbers [3, 5, 7, 9]
4
primes [2, 3, 4]
numbers [5, 7]
5
primes [2, 3, 4, 5]
numbers [5, 7]
6
primes [2, 3, 4, 5, 6]
numbers [7]
7
primes [2, 3, 4, 5, 6, 7]
numbers [7]
8
primes [2, 3, 4, 5, 6, 7, 8]
numbers []
9
primes [2, 3, 4, 5, 6, 7, 8, 9]
numbers []
10
primes [2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers []
numbers at end []
[2, 3, 4, 5, 6, 7, 8, 9, 10]

Any suggestions and comments are most welcome!. Thanks

3 Answers 3

2

Rebinding the name doesn't change the iterable being iterated over. Consider rewriting the code to use a while loop instead.

Sign up to request clarification or add additional context in comments.

Comments

2

Try to write your code simple and straight forward. Overcomplexity always leads to confusions. Your algorithm can be translate into a two line function.

>>> def getprimes(n):
         numbers = [ i for i in range (2,n+1)]
         primes=[k for k in numbers if all((k%i!=0) for i in numbers if i!=k)]
         return primes

>>> print getprimes(10)
[2, 3, 5, 7]

>>> print getprimes(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

2 Comments

+1. can you explain how the keyword all is used? It is nice you could write this in just two lines of code.
@user1988876, all(some_iterable) basically returns True if all elements of the iterable (e.g. a list) are True. So, that line means, if (k%i!=0) is True for all the numbers in the list numbers, returns True. Of course, we want to exclude k for the list numbers, that's what the final if i!=k is here for.
1

Try this, as suggested by Ignacio, try the while loop instead as

def getprimes(n):
     numbers = [ i for i in range (2,n+1)]
     primes=[]

     while (numbers):
        print numbers[0]
        primes.append(numbers[0])
        print "primes",primes
        numbers=[k for k in numbers if k%numbers[0]!=0 ]
        print numbers

     print numbers
     return primes

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.