Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89565

Lucky numbers in Python

Path csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!137.226.75.22.MISMATCH!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.pionier.net.pl!feed.xsnews.nl!border03.ams.xsnews.nl!feeder04.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F09-19.ams.news.kpn.nl
From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Lucky numbers in Python
Organization Decebal Computing
X-Face "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR<J%a^F2lh4[N~Yz4 nSp#c+aQo1b5=?HcNEkQ7QzF<])O3X4MDL/AYjys&*mt>,v+Pti8=Vi/Z"g^?b"E
X-Homepage http://www.decebal.nl/
Date Wed, 29 Apr 2015 20:24:02 +0200
Message-ID <87lhhabxod.fsf@Equus.decebal.nl> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:x5A2qv8L3jLJIkFifK90/HYpZfY=
MIME-Version 1.0
Content-Type text/plain
Lines 27
NNTP-Posting-Host 81.207.62.244
X-Trace 1430332129 news.kpn.nl 22930 81.207.62.244@kpn/81.207.62.244:45082
Xref csiph.com comp.lang.python:89565

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 20:24 +0200
  Re: Lucky numbers in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 13:57 -0600
    Re: Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 23:45 +0200
      Re: Lucky numbers in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 16:38 -0600
        Re: Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 02:01 +0200
          Re: Lucky numbers in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 20:55 -0600
            Re: Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 08:34 +0200
      Re: Lucky numbers in Python Chris Kaynor <ckaynor@zindagigames.com> - 2015-04-29 15:56 -0700
    Re: Lucky numbers in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-30 10:11 +1000
      Re: Lucky numbers in Python Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-29 21:08 -0600
  Re: Lucky numbers in Python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 20:55 +0200
    Re: Lucky numbers in Python Dave Angel <davea@davea.name> - 2015-04-30 15:28 -0400

csiph-web