Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'python': 0.08; 'calculates': 0.09; 'googled': 0.09; 'iterate': 0.09; 'pm,': 0.10; 'output': 0.11; '>>>': 0.12; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; '(ie,': 0.16; 'assignment?': 0.16; 'caches': 0.16; 'constructed': 0.16; 'factor,': 0.16; 'rebert': 0.16; 'url:svn': 0.16; 'url:trunk': 0.16; '\xa0do': 0.16; '\xa0in': 0.16; 'algorithm': 0.16; 'cc:addr:python-list': 0.17; 'language': 0.18; 'header:In-Reply-To:1': 0.21; "haven't": 0.22; "aren't": 0.22; 'loop': 0.22; 'thu,': 0.22; 'trying': 0.23; '(on': 0.23; 'division': 0.23; '\xa0if': 0.23; 'runs': 0.23; "what's": 0.23; 'creating': 0.24; 'code': 0.24; 'values': 0.25; '"this': 0.25; 'pointed': 0.25; 'function': 0.25; 'moving': 0.26; 'url:mailman': 0.26; "i'm": 0.27; 'work.': 0.28; 'wondering': 0.28; 'message- id:@mail.gmail.com': 0.28; 'problem': 0.28; 'random': 0.28; 'import': 0.29; 'around.': 0.29; 'disk': 0.29; 'least': 0.30; 'cc:addr:python.org': 0.30; 'url:listinfo': 0.30; 'cc:addr:gmail.com': 0.30; 'module': 0.30; 'it.': 0.31; 'this.': 0.31; 'done': 0.32; 'yet': 0.32; 'does': 0.33; 'break': 0.33; 'list': 0.33; 'asking': 0.33; 'things': 0.33; 'received:209.85.212': 0.34; 'chris': 0.34; 'weeks': 0.34; 'there': 0.35; 'test': 0.35; 'quite': 0.36; 'probably': 0.36; 'table': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'bigger': 0.37; 'specially': 0.37; 'pretty': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'but': 0.38; 'cc:2**2': 0.38; 'smaller': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'received:209': 0.39; 'finding': 0.39; 'appreciated.': 0.40; 'really': 0.40; 'simply': 0.60; 'your': 0.60; 'our': 0.63; 'exact': 0.65; 'dealing': 0.69; 'benefit': 0.70; 'encountered': 0.73; 'prime': 0.73; 'thousand': 0.74; 'subject:this': 0.76; 'trial': 0.76; 'million': 0.77; 'subject:..': 0.82; '02:59': 0.84; '10:55': 0.84; 'bigger,': 0.84; 'num': 0.84; 'permitted': 0.84; 'factors': 0.91; 'fifty': 0.91; 'received:209.85.212.178': 0.91; 'received:mail-px0-f178.google.com': 0.91; 'cc:no real name:2**2': 0.95 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:cc:content-type :content-transfer-encoding; bh=rng9yhvwaNP5ijGYMQjGGr7vlnyE5nSEsZMbw34KRAo=; b=Y1YNpVh6XO/k788y6d3kkYDJzbLjsdnHYbJ10XaZYJjWz34pMClYEu46PWuWCrnOzK D+FkIyvHZHQCW0CC7EXDuhH08ZPeNXrny2xjGXqDOOZS0RsuTHgyREzZx6GDsn4vobhf 4NzMfbd0Hp2oOA+MebH6Bv4WfIi1FPN4q0yGU= 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 :cc:content-type:content-transfer-encoding; b=HRbP+JUBYmfXH7jByubSFxoafUk1vcnnKoM/g9Qm1hVulsYIY6OALshKnmDJiJbFaE DkCC+dCXppE2BI0Xdp1nDrDLTIUbDBYK89oo/gon6KEuCY5uOHHK+aeEh6iuuz291+ol g4T89RXSVqXMKSxpD7Ln41pbGJ6kBW41c3zwg= MIME-Version: 1.0 In-Reply-To: References: <4DF0B0BE.3020907@ieee.org> Date: Fri, 10 Jun 2011 01:10:28 -0700 Subject: Re: Any Better logic for this problem.. From: geremy condra To: Dan Stromberg Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, sganapathy.subramanium@gmail.com, davea@ieee.org 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: 116 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307693432 news.xs4all.nl 49177 [::ffff:82.94.164.166]:53680 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7363 On Thu, Jun 9, 2011 at 6:10 PM, Dan Stromberg wrote: > > On Thu, Jun 9, 2011 at 10:55 AM, geremy condra wrote= : >> >> On Thu, Jun 9, 2011 at 4:38 AM, Dave Angel wrote: >> > On 01/-10/-28163 02:59 PM, Chris Rebert wrote: >> >> >> >> On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium >> >> =A0wrote: >> >>> >> >>> Hi Guru's, >> >>> I'm working on a solution to find the prime factor of the number >> >>> This part of the code works.. http://www.pastie.org/2041584 >> >>> >> >>> When the number gets bigger, the range cannot iterate through bigger >> >>> number >> >>> and it does not work. >> >>> When I googled , I came across creating our own range function to >> >>> solve >> >>> this. I was just wondering if there was a better algorithm to get th= e >> >>> prime >> >>> numbers / prime factors of a long number? >> >>> >> >>> Any inputs is highly appreciated. >> >> >> > >> > Others have pointed out various inefficiencies. But I wanted to start = by >> > asking what this is for. =A0Do you really have a need to factor number= s >> > over 2 >> > billion? =A0Repeatedly? =A0In multiple runs of the program? =A0Do you = have >> > weeks >> > of computer time to spend or just hours? =A0Are you really interested = in >> > the >> > factors, or just whether or not a particular large number is prime >> > (=3D=3Dhas >> > anyfactors) ? =A0If this is a homework assignment, what's the exact >> > assignment? =A0Are you permitted to use other libraries, or other >> > languages? >> > =A0Are you permitted to use language features you haven't encountered = yet >> > in >> > class? >> >> My solution: >> >> def factors(x): >> =A0 status, output =3D subprocess.getstatusoutput('factor %d' % x) >> =A0 if not status: >> =A0 =A0 =A0 =A0return [int(i) for i in output.split()[1:]] >> =A0 else: >> =A0 =A0 =A0 =A0print(output) >> >> Works pretty well. >> >> >> >> > So you should probably turn the problem around. =A0Design a function t= hat >> > calculates the nth prime, but that caches the work it's already done (= on >> > disk if appropriate, but in a list if not). =A0In the loop that's find= ing >> > the >> > factors, simply call the first function each time, and each time you >> > find a >> > factor, divide num by that so you're dealing with a smaller number. >> >> Just use a precomputed table to do your trial division. There's a list >> of the first fifty million primes on prime pages- if you aren't >> dealing with specially constructed values (ie, RSA moduli) and haven't >> found a factor by the end of the first ten thousand or so you probably >> need to do a primality check before moving on to trying to factor it. >> >> Geremy Condra >> -- >> http://mail.python.org/mailman/listinfo/python-list > > You Might be able to benefit from a primality test like Miller-Rabin, at > least if your numbers can be really large.=A0 It can answer with "this nu= mber > is definitely composite" or "this number is probably prime".=A0 For quite > large numbers, it might speed things up.=A0 For smaller numbers, trial > division is faster. > > I have a Python Miller-Rabin module at: > > http://stromberg.dnsalias.org/svn/big-prime/trunk/ Here's a non-gmpy randomized MR implementation: import random def miller_rabin(n, confidence=3D20): t, s, d =3D n-1, 0, 0 while not t % 2: t =3D t >> 1 s +=3D 1 t, d =3D n-1, t for i in range(confidence): a =3D random.randrange(2, n) x =3D pow(a, d, n) if x =3D=3D 1: continue if x =3D=3D t: continue for r in range(1, s): x =3D pow(x, 2, n) if x =3D=3D t: break if x =3D=3D 1: return False else: return False return True