Any Better logic for this problem..

Chris Rebert clp2 at rebertia.com
Thu Jun 9 05:06:06 EDT 2011


On Thu, Jun 9, 2011 at 1:31 AM, Ganapathy Subramanium
<sganapathy.subramanium at 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.



More information about the Python-list mailing list