Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!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.137 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.73; '*S*': 0.00; 'am,': 0.14; 'wrote:': 0.14; 'algorithmic': 0.16; 'angelico': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'stop.': 0.16; 'header:In- Reply-To:1': 0.21; 'gregory': 0.23; 'received:209.85.210.174': 0.23; 'received:mail-iy0-f174.google.com': 0.23; 'fri,': 0.23; 'message-id:@mail.gmail.com': 0.28; 'instead': 0.29; 'advantageous': 0.30; 'ewing': 0.30; 'clearly': 0.32; 'to:addr :python-list': 0.33; 'list': 0.33; 'rather': 0.34; 'chris': 0.34; 'thinking': 0.34; 'however,': 0.34; 'describe': 0.34; 'quite': 0.36; 'received:google.com': 0.37; 'change': 0.37; 'received:209.85': 0.37; 'extremely': 0.37; 'but': 0.38; 'subject:: ': 0.38; 'received:209': 0.39; 'either': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'stop': 0.62; 'here.': 0.69; 'divided': 0.73; 'prime': 0.73; 'subject:this': 0.76; 'subject:..': 0.82; '2...': 0.84; 'factors': 0.91 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=W24XUVWVmbAYPBImPwYwOqHolGbM1VYLSZJPgzmxNtQ=; b=tAbaiqt0MB/r1wAIjXjydV1NlZWQN+dP9+fhqwfFoFB6QjLyXPB2uOPF0XjBGqq6yr rgQ2mYkXzpVdy/c8v8OBQyKMfcW2PoFo8GNq8edAgOrRL7oUf+WlrXwLM7lYG6DQ7qfV TOEHUqfOGW/l/hGNON3RmoVLwVEZrAC06xGYU= 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=c3L3NkzV6OpZOkeReEzLJlY+IQZxYgG8i4leRrvPFtvHM2HCz9azYk2b/ttewq6KxD EmCzVw5K9SsGo8isfo0AQwoW8eSSgpzuZZUhfPsiSijRWwR4dvtk8GRz2imp1WVo7lt1 UpXXr1winXTdJH8FLmEZn5wDDC/BYeUd6ccLM= MIME-Version: 1.0 In-Reply-To: <95d0drFa9jU1@mid.individual.net> References: <95d0drFa9jU1@mid.individual.net> Date: Fri, 10 Jun 2011 08:44:46 +1000 Subject: Re: Any Better logic for this problem.. 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: 25 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307659489 news.xs4all.nl 49039 [::ffff:82.94.164.166]:49653 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7326 On Fri, Jun 10, 2011 at 8:39 AM, Gregory Ewing wrote: > Chris Angelico wrote: > >> Rather than find all prime numbers up to num, stop at sqrt(num) - it's >> not possible to have any prime factors larger than that. > > That's not quite true -- the prime factors of 26 are 2 and 13, > and 13 is clearly greater than sqrt(26). Oops! My bad. I was thinking in terms of the "divide and conquer" algorithm, whereby the 13 would be the residuum after dividing by 2... > However, once you've divided out all the primes up to sqrt(n), > whatever is left, if greater than 1, must itself be prime, so > you can add it to your prime factors and stop. ... which is effectively the same as you describe here. It's a small algorithmic change but an extremely advantageous one. If you _don't_ look for the residuum, then you stop at n/2 instead of sqrt(n). Either way, though, you don't need to list the primes all the way up to n, which will improve performance significantly. Chris Angelico