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


Groups > comp.lang.python > #7281

Re: Any Better logic for this problem..

References <BANLkTinWq5pjX2+0OVRvm1Xs7ESmYf0qcw@mail.gmail.com>
Date 2011-06-09 02:06 -0700
Subject Re: Any Better logic for this problem..
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.46.1307610371.11593.python-list@python.org> (permalink)

Show all headers | View raw


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

For the archives, that code is:

num = 13195
#num = 600851475143L
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.

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


Thread

Re: Any Better logic for this problem.. Chris Rebert <clp2@rebertia.com> - 2011-06-09 02:06 -0700

csiph-web