-1

I am very new to Python, and programming in general. I am trying to write a program that spits out prime factors of a large number. I wrote a code that gives me prime numbers, but it takes way too long to execute:

for b in range("really large number"): if b%2!=0 and b%3!=0 and b%5!=0 and b%7!=0 or b==2 or b==3 or b==5 or b==7: print b

If anyone has some ideas of how to speed it up let me know. Also, if someone could point me in the right direction to using those prime numbers to find that same "really large number"'s prime factors, I'd really appreciate it. Thanks

Gnardog
  • 133
  • 6
  • Did you search for this? There's lots and lots of info on SO and otherwise available for this problem. – tom10 Feb 10 '16 at 19:26
  • 1
    [This](http://stackoverflow.com/questions/3939660/sieve-of-eratosthenes-finding-primes-python ) should help :) . Try using sieve of eratosthenes. – lazy rabbit Feb 10 '16 at 19:26
  • Possible duplicate of [Fastest way to list all primes below N](http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n) – Vitor M. Barbosa Feb 10 '16 at 20:16

1 Answers1

1

This should work for you:

def print_factors(bigNo):
        for i in range(2, bigNo):
            while bigNo % i == 0:
                print i,
                bigNo /= i
            if bigNo <= 1:
                break

    print_factors(15) # 3 5
    print_factors(12345) # 3 5 823 
Garrett R
  • 2,549
  • 8
  • 14
  • this code works great. I am trying to work with numbers in the hundreds of billions. I get `killed 9` when i try to run it – Gnardog Feb 11 '16 at 04:02
  • maybe try replacing range with xrange? you can also select it as an answer if it works for you – Garrett R Feb 11 '16 at 04:09
  • xrange for sure helps, but I'm still trying to speed it up more. Maybe trying to have it sift through prime numbers instead of all numbers between 2 and n. So thats my next goal I guess – Gnardog Feb 11 '16 at 18:42