Use sympy for faster factorization if available (#199)

This commit is contained in:
Lonami Exo
2017-08-17 17:37:27 +02:00
parent 7e85a3cda4
commit 9f812d83a9
2 changed files with 14 additions and 2 deletions

View File

@@ -1,4 +1,8 @@
from random import randint
try:
import sympy.ntheory
except ImportError:
sympy = None
class Factorization:
@@ -58,5 +62,8 @@ class Factorization:
@staticmethod
def factorize(pq):
"""Factorizes the given number and returns both the divisor and the number divided by the divisor"""
divisor = Factorization.find_small_multiplier_lopatin(pq)
return divisor, pq // divisor
if sympy:
return tuple(sympy.ntheory.factorint(pq).keys())
else:
divisor = Factorization.find_small_multiplier_lopatin(pq)
return divisor, pq // divisor