Finding large prime numbers with python

This post will provide how to calculate the largest prime number of a 96 bit number. This was chosen as a starting point. As you can see, it took almost 27 minutes (1614 seconds) to calculate on a dual core laptop.

>>> import random
>>> random.getrandbits(64)
5586838809177767755L
>>> def calcPrime(n):
...   i = 2
...   while i * i <= n:
...     if n % i:
...       i += 1
...     else:
...       n //= i
...   return n
...
>>> calcPrime(random.getrandbits(64))
17132851387L
>>> calcPrime(random.getrandbits(70))
29976485827L
>>> calcPrime(random.getrandbits(80))
83358363197L
>>> import time
>>> t0 = time.time()
>>> calcPrime(random.getrandbits(96))
>>> t1 = time.time()
>>> print t1-t0
1614.5999999
>>>

A security algorithm such as RSA uses the largest prime factors of a composite number. As such, you would have to calculate each number for a number much higher than can be stored in 96 bits (roughly 79 octillion); in 2016, 2048 bit encryption is standard.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.