Describe to me a procedure for finding the prime factors of any number, in simple steps.
bonus if I can actually execute it in Python.
Python is executable pseudocode
this is simple man .. what have you tried ?
I can do it in pseudo-c, but not python. for (i=1; i<sqrt{x}; i++) { if x%i == 0 { cout << x << " is not prime, yo! " << ; } }
@Dalvoron:Incorrect solution
that's just the test for primality, not code for prime factorization. :-P
Aha, misread!
def primef(n): i = 2 ans = [] while n > 1: if n % i == 0: n /= i ans.append(i) else: for i in xrange(i, n+1): if i**2 < n and n % i == 0: break return ans is this right?
for prime numbers it just returns the prime number alone
It's not very optimized :(
the prime factorization of 999 is 3, 3, 3, and 37
The prime factorization of 24389 is 29*29*29
the prime factorization of 23897235782673 is : OverflowError: Python int too large to convert to C long
how do I fix it?
what's your purpose for solving this problem?
just for fun :-D I wanna know a great way of doing it.
then I can port it to my calculator as an efficient C program.
I have a very optimized solution using miller rabin and pollard rho but it's for www.spoj.pl/problems/FACT0/ www.spoj.pl/problems/FACT2/ www.spoj.pl/problems/FACT1/ Hence, I can't post here
:(
how do I fix my int too large for C long problem
that's exactly what the spoj problem is about.
so I need to write a bigint class if I am ever gonna port it to C?
Yes
but that would slow things down a little :( walk through the linked list
No.. learn properly first :)
Join our real-time social learning platform and learn together with your friends!