Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'value,': 0.03; 'subject:Python': 0.05; 'intermediate': 0.05; 'suppose': 0.07; 'python': 0.09; '"if': 0.09; 'array.': 0.09; 'coding,': 0.09; 'internally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'slightly': 0.15; '(starting': 0.16; '107': 0.16; '2),': 0.16; '524': 0.16; 'basic.': 0.16; 'max:': 0.16; 'original:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sequence:': 0.16; 'subject:3.3': 0.16; 'surprising': 0.16; 'wrote:': 0.17; 'odd': 0.17; 'pace': 0.17; 'skip': 0.17; 'jan': 0.18; 'solution.': 0.18; 'app': 0.19; 'equivalent': 0.20; 'bit': 0.21; 'import': 0.21; 'machine.': 0.22; 'produces': 0.22; 'defined': 0.22; 'runs': 0.22; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'url:wiki': 0.26; 'values': 0.26; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'coded': 0.29; 'surprised': 0.29; 'url:wikipedia': 0.29; 'array': 0.29; 'starts': 0.29; "skip:' 10": 0.30; 'usually': 0.30; 'basic': 0.30; 'code': 0.31; '(and': 0.32; 'problem': 0.33; 'to:addr :python-list': 0.33; 'project': 0.34; 'compared': 0.35; 'sequence': 0.35; 'pm,': 0.35; 'too.': 0.35; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'url:org': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'positive': 0.38; 'url:en': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'step': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'john': 0.60; 'lower': 0.61; 'note:': 0.64; 'here': 0.65; 'reverse': 0.65; 'subject:. ': 0.66; 'saving': 0.72; '1980s': 0.84; 'iterative': 0.84; 'longest': 0.84; 'optimized,': 0.84; 'received:fios.verizon.net': 0.84; 'sec.': 0.84; 'subject:Basic': 0.84; 'timings': 0.84; 'branch,': 0.91; 'good,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Python 3.3 vs. MSDOS Basic Date: Mon, 18 Feb 2013 19:50:51 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 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: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361235083 news.xs4all.nl 6888 [2001:888:2000:d::a6]:58338 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39148 On 2/18/2013 2:13 PM, John Immarino wrote: > I coded a Python solution for Problem #14 on the Project Euler > website. I was very surprised to find that it took 107 sec. to run > even though it's a pretty simple program. I also coded an equivalent > solution for the problem in the old MSDOS basic. (That's the 16 bit > app of 1980s vintage.) It ran in 56 sec. Is there a flaw in my > coding, or is Python really this slow in this particular application. > MSDOS Basic usually runs at a snails pace compared to Python. I find this surprising too. I am also surprised that it even works,=20 given that the highest intermediate value is about 57 billion and I do=20 not remember that Basic had infinite precision ints. > The following iterative sequence is defined for the set of positive > integers: > > n =E2=86=92 n/2 (n is even) n =E2=86=92 3n + 1 (n is odd) Note that if n is odd, 3n + 1 is even (and not 1!), so one may take two=20 steps with (3n + 1)/2. > Using the rule above and starting with 13, we generate the following > sequence: 13 =E2=86=92 40 =E2=86=92 20 =E2=86=92 10 =E2=86=92 5 =E2=86=92= 16 =E2=86=92 8 =E2=86=92 4 =E2=86=92 2 =E2=86=92 1 > > It can be seen that this sequence (starting at 13 and finishing at 1) > contains 10 terms. Although it has not been proved yet (Collatz > Problem), it is thought that all starting numbers finish at 1. https://en.wikipedia.org/wiki/Collatz_conjecture > Which starting number, under one million, produces the longest > chain? I suppose 'print(837799)' would not count as a proper solution. > NOTE: Once the chain starts the terms are allowed to go above one > million. Here is my slightly revised code with timings on a good, 20 month old=20 win 7 machine. from time import time start =3D time() num, max =3D 0, 0 for m in range(1, 1000001): n =3D m count=3D0 while n !=3D1: if n & 1: #n % 2: n =3D (3*n + 1) // 2 count +=3D 2 else: n =3D n//2 count +=3D 1 if count > max: num =3D m max =3D count print(num, max , time()-start) # original: 837799, 524 steps, 53.9 secs # for ... range: 52.3 # reverse inner if 49.0 # double step 39.1 # n & 1 instead of n % 2 for test: 36.0, 36.0, 35.9 # n>>1 instead of n//2: 34.7, 36.1, 36.2; # this may be internally optimized, so skip I do not see any fluff left to remove, unless one takes the major step=20 of saving already calculated values in an array. Since the highest intermediate value of n is 56991483520 (445245965 *2**7, from adding "if n > maxn: maxn =3D n" to the odd branch, before=20 dividing by 2), the array would have to be limited to a much lower=20 value, say a few million. --=20 Terry Jan Reedy