Lucky numbers in Python

Cecil Westerhof Cecil at decebal.nl
Wed Apr 29 20:24:02 CEST 2015


I wrote a function lucky_numbers:
    def lucky_numbers(n):
        if n < 3:
            return [1]
        sieve = range(1, n + 1, 2)
        sieve_index = 1
        while True:
            skip_count  = sieve[sieve_index]
            sieve_len   = len(sieve)
            if sieve_len < skip_count:
                break
            for del_index in range((sieve_len // skip_count) * skip_count - 1, 
                                  skip_count - 2, -skip_count):
                del sieve[del_index]
            sieve_index += 1
        return sieve

I was wondering if there is a way to do this:
            for del_index in range((sieve_len // skip_count) * skip_count - 1, 
                                  skip_count - 2, -skip_count):
                del sieve[del_index]
in a more efficient way.

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



More information about the Python-list mailing list