Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder5.news.weretis.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'instance,': 0.05; 'loop,': 0.07; 'python': 0.07; 'stack,': 0.09; 'subject:error': 0.12; 'syntax': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'coded': 0.16; "function's": 0.16; 'prefer?': 0.16; 'recursive': 0.16; 'variable.': 0.16; 'subject:was': 0.16; 'temporary': 0.19; 'tue,': 0.20; 'header:In-Reply-To:1': 0.22; 'correct,': 0.23; 'received:209.85.214.174': 0.23; 'received:mail- iw0-f174.google.com': 0.23; 'keeps': 0.24; 'expect': 0.26; 'instead': 0.26; 'chris': 0.27; 'pass': 0.27; 'message- id:@mail.gmail.com': 0.28; 'probably': 0.30; 'actually.': 0.31; 'compiling': 0.31; "they'll": 0.31; 'however,': 0.31; 'meaning': 0.31; 'to:addr:python-list': 0.32; 'things': 0.33; 'break': 0.33; 'uses': 0.34; 'difference': 0.35; 'that,': 0.35; 'should': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'pretty': 0.38; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; 'subject: (': 0.39; 'received:209': 0.39; 'similar': 0.40; 'would': 0.40; 'header:Received:5': 0.40; '2011': 0.62; 'overall': 0.64; 'special': 0.66; 'benefit': 0.66; 'believe': 0.66; '11:48': 0.84; 'technically': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=ULNCJ/KP1lh+F75479DGOXX0GoZPda005eqN1kzIsZg=; b=voW1kX8v703VYYgZYa1doMbhYg6DTApWudiyZzcCm4u05n/zsQvHpjQ7lNfgYa8YYX vt49lcxhGZdctoCJXRmZX+2KSQ262zkpoUTcUgAayJTHRIPO6puH8ZwcmqGdxDq8fWXi LKAGLNouhUEoQDn0Lkcx9xcR0HTCVncM/DOEU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=IwdZ/C5HY/CwGkoVJdYLTFbZdXejXv0g1SnCdsndFwW53gV6fn9awKAT9oYilG2JPB 9HEZ2bERf7tUUMoWVz0IsXHeUAMh5fUvmf6GVJWSNAP+w3j3FlPQ/0/CZIZ8aucXyRq+ LrREMyIxBkOEZ50eaYJK054hrqgZsGbFxUZC4= MIME-Version: 1.0 In-Reply-To: References: <0HFvp.18698$vT3.2042@newsfe06.iad> Date: Tue, 3 May 2011 12:13:16 +1000 Subject: Re: Recursion or iteration (was Fibonacci series recursion error) From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 30 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304388800 news.xs4all.nl 41113 [::ffff:82.94.164.166]:51232 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4515 On Tue, May 3, 2011 at 11:48 AM, rusi wrote: > What are their space/time complexities? > Which do you prefer? They're pretty similar actually. If you rework the first one to not use range() but instead have a more classic C style of loop, they'll be almost identical: def powI(x,n): result = 1 while n > 0: result *= x n-=1 return result There's a subtle difference in that powR as you've coded it will infinite-loop if given a negative exponent, while powI in any form will simply return 1 (neither is technically correct, fwiw). Other than that, the only difference is that one keeps its state on the call stack, the other uses a temporary variable. Performance would probably benefit from a special syntax meaning "recurse", which would avoid the LOAD_GLOBAL for the function's name (plus, things break if you pass recursive functions around after compiling them - for instance, powR2=powR; def powR(x,n): os.exit() - but if you do that, then you should expect to see nice bullet-holes in your foot). However, I do not believe that Python would overall benefit from any such thing. Chris Angelico