views:

609

answers:

4

dear all,

i am simulating my crypto scheme in python, i am a new user to it.

p = 512 bit number and i need to calculate largest prime factor for it, i am looking for two things:

  1. Fastest code to process this large prime factorization
  2. Code that can take 512 bit of number as input and can handle it.

I have seen different implementations in other languages, my whole code is in python and this is last point where i am stuck. So let me know if there is any implementation in python.

Kindly explain in simple as i am new user to python

sorry for bad english.

edit (taken from OP's answer below):

#!/usr/bin/env python
def highest_prime_factor(n):
   if isprime(n):
      return n
   for x in xrange(2,n ** 0.5 + 1):
      if not n % x:
         return highest_prime_factor(n/x)

def isprime(n):
   for x in xrange(2,n ** 0.5 + 1):
      if not n % x:
         return False
   return True

if  __name__ == "__main__":
   import time
   start = time.time()
   print highest_prime_factor(1238162376372637826)
   print time.time() - start

The code above works (with a bit of delay) for "1238162376372637826" but extending it to

10902610991329142436630551158108608965062811746392 57767545600484549911304430471090261099132914243663 05511581086089650628117463925776754560048454991130443047

makes python go crazy. Is there any way so that just like above, i can have it calculated it in no time?

+1  A: 

If you can install an extension, gmpy would help -- see my answer to this SO question, specifically the def prime_factors(x) function in the code I show there.

In pure Python (without any extension allowed) it's a tad harder and a lot slower, see the code here for example (but don't hold your breath while it runs on your huge numbers;-).

Alex Martelli
+1  A: 

This should be a better fit then the trivial approach for large numbers (although with this kind of number crunching every pure Python implementation will take a while): Pollard Rho prime factorization.

ChristopheD
The Pollard tests generally don't handle that large of an input in practical time. Rho looks for relatively small factors. P-1 and P+1 algorithms look for primes who are one more or less than a number whose factors are all small. None are adequate for 512 bits today unless you have a special number.
phkahler
@phkahler: thanks for commenting; did not know that. I've always found Pollard to be very fast for (relatively) large numbers, but off course I've never thrown 512 bits at it.
ChristopheD
A: 

('''==============================================================================='''

   '''              CALCULATE  HIGHEST PRIME

FACTOR '''

'''===============================================================================''')

!/usr/bin/env python

def highest_prime_factor(n): if isprime(n): return n for x in xrange(2,n ** 0.5 + 1): if not n % x: return highest_prime_factor(n/x) def isprime(n): for x in xrange(2,n ** 0.5 + 1): if not n % x: return False return True if name == "main": import time start = time.time() print highest_prime_factor(1238162376372637826) print time.time() - start

the code works with a bit of delay on the number : "1238162376372637826" but extending it to (109026109913291424366305511581086089650628117463925776754560048454991130443047109026109913291424366305511581086089650628117463925776754560048454991130443047) makes python go crazy. Is there any way just like above, i can have it calculated it in no time.

miraclesoul
You should have put this in your question (Stackoverflow does not follow the traditional forum flow but is question-answers based). I'll put it in for you, it would be best if you removed this 'answer' then...
ChristopheD
If I had away to factor large primes in "no time", I would be a very rich man.
mikerobi
+1  A: 

For a Python-based solution, you might want to look at pyecm On a system with gmpy installed also, pyecm found the following factors:

101, 521, 3121, 9901, 36479, 300623, 53397071018461, 1900381976777332243781

There still is a 98 digit unfactored composite:

60252507174568243758911151187828438446814447653986842279796823262165159406500174226172705680274911

Factoring this remaining composite using ECM may not be practical.

Edit: After a few hours, the remaining factors are

6060517860310398033985611921721

and

9941808367425935774306988776021629111399536914790551022447994642391

casevh