Path: csiph.com!usenet.pasdenom.info!gegeweb.org!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'subject:Python': 0.06; '"""': 0.07; 'ex:': 0.09; 'exception,': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; 'python3.': 0.16; 'true:': 0.16; 'xrange': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'define': 0.26; 'pass': 0.26; 'defined': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; 'code': 0.31; 'url:wiki': 0.31; 'subject:numbers': 0.31; 'url:wikipedia': 0.31; "i'd": 0.34; 'except': 0.35; 'something': 0.35; 'in:': 0.36; 'url:org': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'is.': 0.60; 'break': 0.61; 'numbers': 0.61; 'range': 0.61; 'charset:windows-1252': 0.65; 'received:74.208': 0.68; 'influence': 0.74; 'now:': 0.74; 'received:74.208.4.194': 0.84; 'lucky': 0.93 Date: Thu, 30 Apr 2015 15:28:56 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Lucky numbers in Python References: <87lhhabxod.fsf@Equus.decebal.nl> <87fv7htpip.fsf@Equus.decebal.nl> In-Reply-To: <87fv7htpip.fsf@Equus.decebal.nl> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:u2YovynHVxWHJSb3eTVHU930VgkudeA6mpvYT2JDbeiB41p9MhS 35w8v/Ipel5cgV9FD+EbWkxAlJr4smxuQi8GK4wDLJMvggtUkaS5h4+6/1d4GvHAuf/ZZPj vVhzGQx4VFR2BiBfK+0Asa6TOr/Jyyd8eWXs32sIb9r1N3sFK9HZHJ0eykkB0IG0VlV4zsn KIF5XyjGzkWPffxlBGDOA== X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430422160 news.xs4all.nl 2871 [2001:888:2000:d::a6]:33506 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89662 On 04/30/2015 02:55 PM, Cecil Westerhof wrote: > 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? > I'd do something like this at top of the module: try: range = xrange except NameError as ex: pass then use range as it is defined in Python3. if that makes you nervous, then define irange = xrange, and if it gets a NameError exception, irange = range -- DaveA