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.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'python,': 0.01; 'else:': 0.03; 'next,': 0.05; 'function,': 0.07; 'worse': 0.07; 'calculates': 0.09; 'googled': 0.09; 'iterate': 0.09; 'loop.': 0.09; 'pm,': 0.10; 'am,': 0.14; 'wrote:': 0.14; 'assignment?': 0.16; 'caches': 0.16; 'factor,': 0.16; 'rebert': 0.16; 'runs,': 0.16; 'algorithm': 0.16; 'cache': 0.16; 'possibly': 0.16; 'cc:addr :python-list': 0.17; 'language': 0.18; 'once,': 0.19; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; "haven't": 0.22; 'loop': 0.22; 'thu,': 0.22; 'trying': 0.23; '(on': 0.23; 'away.': 0.23; 'runs': 0.23; "what's": 0.23; 'creating': 0.24; 'code': 0.24; "doesn't": 0.25; 'pointed': 0.25; 'function': 0.25; "i'm": 0.27; 'work.': 0.28; 'wondering': 0.28; 'problem': 0.28; 'checking': 0.29; 'around.': 0.29; 'disk': 0.29; 'cc:addr:python.org': 0.30; 'cc:addr:gmail.com': 0.30; '(just': 0.30; 'get.': 0.30; 'throwing': 0.30; 'changes': 0.30; 'print': 0.31; 'this.': 0.31; 'done': 0.32; 'yet': 0.32; 'pure': 0.32; 'relatively': 0.32; 'does': 0.33; 'post': 0.33; 'break': 0.33; 'list': 0.33; 'asking': 0.33; 'chris': 0.34; 'weeks': 0.34; 'skip:= 10': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'store': 0.35; 'cc:2**1': 0.35; 'using': 0.35; 'probably': 0.36; 'assuming': 0.37; 'bigger': 0.37; 'ways': 0.37; 'response': 0.37; 'two': 0.37; 'could': 0.38; 'url:org': 0.38; 'but': 0.38; 'smaller': 0.38; 'subject:: ': 0.38; 'received:192': 0.38; 'should': 0.39; 'unless': 0.39; "i'd": 0.39; 'number,': 0.39; 'finding': 0.39; 'list,': 0.39; 'add': 0.39; 'current': 0.40; 'appreciated.': 0.40; 'really': 0.40; 'received:192.168.1': 0.40; 'generate': 0.60; 'more': 0.60; 'simply': 0.60; 'your': 0.60; 'stop': 0.62; 'design': 0.63; 'our': 0.63; 'increase': 0.64; 'exact': 0.65; 'making': 0.67; 'billion': 0.67; 'square': 0.67; 'dealing': 0.69; 'header:Reply-To:1': 0.72; 'reply-to:no real name:2**0': 0.72; 'encountered': 0.73; 'prime': 0.73; 'unnecessary': 0.73; 'subject:this': 0.76; 'future,': 0.76; 'subject:..': 0.82; '02:59': 0.84; 'bigger,': 0.84; 'complex,': 0.84; 'concentrate': 0.84; 'num': 0.84; 'permitted': 0.84; 'factors': 0.91; 'to:none': 0.93 Date: Thu, 09 Jun 2011 07:38:38 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110516 Thunderbird/3.1.10 MIME-Version: 1.0 Subject: Re: Any Better logic for this problem.. References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:DJSK3Xyi9DX6OXmNNkqQ/JA7lr0hXun75sUO0AgADIL UBScnFBjfmOyDY/caFQaFOTD9G8rzcr9VvghVCo5ykGRY6SZRu Xw4mQZI4h3d8gNMK/Glq49SCnTjZOmDWI4+c7nkvy7RbrR7sUN g9BGMYonUyecyLxZHFhI0sz5XhwQOUAtEvOOLzbc4ysT2/0FWc qG/dIeIMXCP7wQbD2jQ3A== Cc: python-list@python.org, sganapathy.subramanium@gmail.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: davea@ieee.org 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: 81 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307619540 news.xs4all.nl 49180 [::ffff:82.94.164.166]:37633 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7291 On 01/-10/-28163 02:59 PM, Chris Rebert wrote: > On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium > 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 > > For the archives, that code is: > > num =3195 > #num =00851475143L > prime_numbers =2] > prime_factors =] > > for i in range (2,num): > for j in prime_numbers: > if i % j =0: > break > else: > prime_numbers.append(i) > > print 'The Prime Numbers are : ', prime_numbers > for items in prime_numbers: > if num % items =0: > prime_factors.append(items) > > print 'The prime factors are : ' , prime_factors > > > In the future, please avoid the unnecessary indirection of pastebins > when your code is relatively short. Including the code directly in > your post is also likely to increase the response rate you get. > > Cheers, > Chris > >> 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? Assuming you have to use pure python, no extra libraries, nothing complex, I'd just concentrate on making the current program efficient, without major surgery. First, you're generating far more primes than you can possibly need. You could stop at the square root of num. Next, you're trying every number, but you could be checking every other number (just add a step value to the range). Those two changes eliminate the range() problem, as the sqrt doesn't get over 2 billion till the num is over 10**18. But more fundamentally, you're generating a big list of numbers, using part of it once, and throwing it away. You could cache that list, store it on disk between runs, and make it tons faster. Worse you probably don't even need anywhere near the sqrt, unless num is prime. 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. There are faster ways to generate the primes, but now those optimizations can be applied to the nthprime() function, and they're independent of the factorization loop. DaveA