Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.05; 'cache': 0.05; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; "wouldn't": 0.11; '1),': 0.16; 'cache:': 0.16; 'purposes.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:3.3': 0.16; 'table))': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'tim': 0.18; 'feb': 0.19; "i'd": 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'header:X-Complaints-To:1': 0.28; '>>>>': 0.29; 'seconds': 0.30; 'could': 0.32; 'to:addr:python-list': 0.33; 'table': 0.35; 'received:org': 0.36; 'but': 0.36; 'enough': 0.36; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'received:46': 0.60; 'most': 0.61; 'share': 0.61; 'subject:. ': 0.66; 'further,': 0.71; '2013': 0.84; 'subject:Basic': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Serhiy Storchaka Subject: Re: Python 3.3 vs. MSDOS Basic Date: Tue, 19 Feb 2013 22:28:25 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 46.211.170.33 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361305727 news.xs4all.nl 6840 [2001:888:2000:d::a6]:43006 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39271 On 19.02.13 20:31, Ian Kelly wrote: > On Tue, Feb 19, 2013 at 7:46 AM, Tim Daneliuk wrote: >> Are you sure you wouldn't like to share with the class? I'd be interested >> in seeing your approach... > > Very well: > > def collatz(n, memo): > if n not in memo: > if n % 2 == 0: > next_n = n // 2 > else: > next_n = 3 * n + 1 > memo[n] = collatz(next_n, memo) + 1 > return memo[n] > > def run_collatz(upper): > table = {1: 0} > max_n = max(range(1, upper), key=lambda n: collatz(n, table)) > return max_n, table[max_n] > >>>> run_collatz(1000000) > (837799, 524) > > It could certainly be optimized further, but at about 4 seconds it's > already fast enough for most purposes. 10-15% faster: def f(M): def g(n, cache = {1: 0}): if n in cache: return cache[n] if n % 2: m = 3 * n + 1 else: m = n // 2 cache[n] = count = g(m) + 1 return count num = max(range(2, M + 1), key=g) return num, g(num) print(*f(1000000))