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


Groups > comp.lang.python > #7311

Re: Any Better logic for this problem..

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!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.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; '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; '\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; '\xa0if': 0.23; 'runs': 0.23; "what's": 0.23; 'creating': 0.24; 'code': 0.24; 'values': 0.25; 'pointed': 0.25; 'function': 0.25; 'moving': 0.26; "i'm": 0.27; 'work.': 0.28; 'wondering': 0.28; 'message-id:@mail.gmail.com': 0.28; 'problem': 0.28; 'around.': 0.29; 'disk': 0.29; 'cc:addr:python.org': 0.30; 'cc:addr:gmail.com': 0.30; 'it.': 0.31; 'this.': 0.31; 'done': 0.32; 'yet': 0.32; 'does': 0.33; 'list': 0.33; 'asking': 0.33; 'received:209.85.212': 0.34; 'chris': 0.34; 'weeks': 0.34; 'there': 0.35; 'cc:2**1': 0.35; '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:org': 0.38; 'but': 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; '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; '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
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=Gm41QXZefRtD0/rH186z/esvFvKpHHxmVkgsqv2tl2o=; b=aVyRQPJervi+cbwOdNImfe1gX/YXiUGrqUXHx8u7mfexxzDGB0JkZjWohaHsja9BD1 RuCN0XMyASFbojFCVoN7v4pCAX0ClvvoqQKrlPqHGbD+iITrar/6HdG+SusqnZ0c4S30 OlG+AODFOHoIfXJfyNwH0RRcsX7D0qy7x4iB4=
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=U/aWL7LvYMfXDLxkEUxs9WEcEubJ9JtkQ+ksLMVuoI6XFAKaZAY01sRQaNiQxOsSDQ FQmjv3HJ4tA1X2ZQ+ZhIqHTH+nRhfEBLnWk/orlyC3NWwxtvoNQtwYBy5dNNH0G8ybg6 xuJmezHYA/qjszvYO4IvvmTCK1R0pf0hXe52E=
MIME-Version 1.0
In-Reply-To <4DF0B0BE.3020907@ieee.org>
References <BANLkTinWq5pjX2+0OVRvm1Xs7ESmYf0qcw@mail.gmail.com> <BANLkTimicVsm7EGMsVu0bXRsa7714MKpwA@mail.gmail.com> <4DF0B0BE.3020907@ieee.org>
Date Thu, 9 Jun 2011 10:55:04 -0700
Subject Re: Any Better logic for this problem..
From geremy condra <debatem1@gmail.com>
To davea@ieee.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org, sganapathy.subramanium@gmail.com
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.57.1307642108.11593.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 82.94.164.166
X-Trace 1307642108 news.xs4all.nl 49047 [::ffff:82.94.164.166]:45204
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7311

Show key headers only | View raw


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

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-09 10:55 -0700

csiph-web