Lucky numbers in Python

Cecil Westerhof Cecil at decebal.nl
Thu Apr 30 20:55:10 CEST 2015


Because I want the code to work with Python 3 also, the code is now:
    def lucky_numbers(n):
        """
        Lucky numbers from 1 up-to n
        http://en.wikipedia.org/wiki/Lucky_number
        """

        if n < 3:
            return [1]
        sieve = list(range(1, n + 1, 2))
        sieve_index = 1
        while True:
            sieve_len   = len(sieve)
            if (sieve_index + 1) > sieve_len:
                break
            skip_count  = sieve[sieve_index]
            if sieve_len < skip_count:
                break
            del sieve[skip_count - 1 : : skip_count]
            sieve_index += 1
        return sieve

It looks like the list in:
        sieve = list(range(1, n + 1, 2))

does not have much influence in Python 2. So I was thinking of leaving
the code like it is. Or is it better to check and do the list only
with Python 3?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list