Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.212.46': 0.03; 'received:mail- vw0-f46.google.com': 0.03; 'elif': 0.05; 'linear': 0.07; 'written.': 0.07; 'pm,': 0.11; 'subject:error': 0.12; 'def': 0.13; 'wrote:': 0.14; '-n)': 0.16; 'n),': 0.16; 'recursive': 0.16; 'subject:was': 0.16; 'simpler': 0.19; 'header:In-Reply-To:1': 0.22; 'mon,': 0.22; 'version.': 0.23; 'received:209.85.212': 0.25; 'message-id:@mail.gmail.com': 0.28; 'fact': 0.31; 'to:addr:python- list': 0.32; 'bit': 0.33; 'actually': 0.34; 'difference': 0.35; 'think': 0.36; 'received:209.85': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'to:addr:python.org': 0.39; 'could': 0.39; 'subject: (': 0.39; 'received:209': 0.39; 'solution': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'might': 0.40; '2011': 0.62; 'harder': 0.65; 'benefit': 0.66; '11:27': 0.84; 'complexity': 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:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=jryeTlfYDuMk+Ms5jZfcy9Dzn0HNPW6IPKBR08e0y5w=; b=lI/qDvGZ3QuhH4vihP8A41JItK6PRlnwaLdugRjtBq9BaLrJXZr++kxs/DElrEDRR9 kFqi7FIO0Ok/TyRXLi3zgE7CyYjHAB6SoG7pCXMftBCAsSsxUJkLjB0hvy1f932VQGog b/nVUfrqU2DMfQV6W/37eR2mZgFokEFlj0Vzg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=Ze35zxcHqreQOFxdSouXE2GltIq56tDeD+ToXpMj6y4cU6+q1MXZT3sT1/p+58BGxZ b0LZIHSxzEK9jYRQEwkXn4ypP54Wh6u9nXBJhBIjbxktH1cnDchDPMkL920rYMSKer3T A0gylljJMxVic6aqxqwC9DqgnGfL2Jxo1d8vU= MIME-Version: 1.0 In-Reply-To: References: <0HFvp.18698$vT3.2042@newsfe06.iad> From: Ian Kelly Date: Mon, 2 May 2011 23:46:58 -0600 Subject: Re: Recursion or iteration (was Fibonacci series recursion error) To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 27 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304401651 news.xs4all.nl 41117 [::ffff:82.94.164.166]:36855 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4524 On Mon, May 2, 2011 at 11:27 PM, Dan Stromberg wrote: > But the recursive solution has time complexity of O(logn).=A0 The iterati= ve > solution has time complexity of O(n).=A0 That's a significant difference = for > large n - a significant benefit of the recursive version. It's linear as written. I think you're talking about an implementation like this one: def powR(x,n): if n < 0: return 1.0 / powR(x, -n) elif n =3D=3D 0: return 1 else: half_n =3D n // 2 root =3D powR(x, half_n) result =3D root * root if half_n + half_n =3D=3D n - 1: result =3D result * x return result That's O(log n), but it was harder to write, and it could just as easily be iterative. In fact it might actually have been a bit simpler to write it iteratively.