Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7363

Re: Any Better logic for this problem..

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 <debatem1@gmail.com>
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 <BANLkTin2BEBJMuTvDWr5w0b_BX3zcFOa=g@mail.gmail.com>
References <BANLkTinWq5pjX2+0OVRvm1Xs7ESmYf0qcw@mail.gmail.com> <BANLkTimicVsm7EGMsVu0bXRsa7714MKpwA@mail.gmail.com> <4DF0B0BE.3020907@ieee.org> <BANLkTinv6kB5e0T+r7XmTiUaV3sGH0CAYQ@mail.gmail.com> <BANLkTin2BEBJMuTvDWr5w0b_BX3zcFOa=g@mail.gmail.com>
Date Fri, 10 Jun 2011 01:10:28 -0700
Subject Re: Any Better logic for this problem..
From geremy condra <debatem1@gmail.com>
To Dan Stromberg <drsalists@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.80.1307693432.11593.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Thu, Jun 9, 2011 at 6:10 PM, Dan Stromberg <drsalists@gmail.com> wrote:
>
> On Thu, Jun 9, 2011 at 10:55 AM, geremy condra <debatem1@gmail.com> wrote:
>>
>> On Thu, Jun 9, 2011 at 4:38 AM, Dave Angel <davea@ieee.org> wrote:
>> > On 01/-10/-28163 02:59 PM, Chris Rebert wrote:
>> >>
>> >> On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium
>> >> <sganapathy.subramanium@gmail.com>  wrote:
>> >>>
>> >>> 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 the
>> >>> 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.  Do you really have a need to factor numbers
>> > over 2
>> > billion?  Repeatedly?  In multiple runs of the program?  Do you have
>> > weeks
>> > of computer time to spend or just hours?  Are you really interested in
>> > the
>> > factors, or just whether or not a particular large number is prime
>> > (==has
>> > anyfactors) ?  If this is a homework assignment, what's the exact
>> > assignment?  Are you permitted to use other libraries, or other
>> > languages?
>> >  Are you permitted to use language features you haven't encountered yet
>> > in
>> > class?
>>
>> My solution:
>>
>> def factors(x):
>>   status, output = subprocess.getstatusoutput('factor %d' % x)
>>   if not status:
>>        return [int(i) for i in output.split()[1:]]
>>   else:
>>        print(output)
>>
>> Works pretty well.
>>
>> <snip>
>>
>> > So you should probably turn the problem around.  Design a function that
>> > calculates the nth prime, but that caches the work it's already done (on
>> > disk if appropriate, but in a list if not).  In the loop that's finding
>> > 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.  It can answer with "this number
> is definitely composite" or "this number is probably prime".  For quite
> large numbers, it might speed things up.  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=20):
	t, s, d = n-1, 0, 0
	while not t % 2:
		t = t >> 1
		s += 1
	t, d = n-1, t

	for i in range(confidence):
		a = random.randrange(2, n)
		x = pow(a, d, n)
		if x == 1: continue
		if x == t: continue
		for r in range(1, s):
			x = pow(x, 2, n)
			if x == t: break
			if x == 1: return False
		else: return False
	return True

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Any Better logic for this problem.. geremy condra <debatem1@gmail.com> - 2011-06-10 01:10 -0700

csiph-web