2018-12-16 17:09:54 +08:00
|
|
|
#coding: utf-8
|
|
|
|
''' mbinary
|
|
|
|
#######################################################################
|
|
|
|
# File : factorize.py
|
|
|
|
# Author: mbinary
|
|
|
|
# Mail: zhuheqin1@gmail.com
|
2019-01-31 12:09:46 +08:00
|
|
|
# Blog: https://mbinary.xyz
|
2018-12-16 17:09:54 +08:00
|
|
|
# Github: https://github.com/mbinary
|
|
|
|
# Created Time: 2018-12-16 09:36
|
|
|
|
# Description: factorization, using pollard's rho algorithm and miller-rabin primality test
|
|
|
|
#######################################################################
|
|
|
|
'''
|
|
|
|
|
|
|
|
from random import randint
|
|
|
|
from isPrime import isPrime
|
2020-04-15 12:28:20 +08:00
|
|
|
from gcd import gcd
|
|
|
|
|
2018-12-16 17:09:54 +08:00
|
|
|
|
|
|
|
def factor(n):
|
|
|
|
'''pollard's rho algorithm'''
|
2020-04-15 12:28:20 +08:00
|
|
|
if n < 1:
|
|
|
|
raise Exception('[Error]: {} is less than 1'.format(n))
|
|
|
|
if n == 1:
|
|
|
|
return []
|
|
|
|
if isPrime(n):
|
|
|
|
return [n]
|
|
|
|
fact = 1
|
|
|
|
cycle_size = 2
|
2018-12-16 17:09:54 +08:00
|
|
|
x = x_fixed = 2
|
2020-04-15 12:28:20 +08:00
|
|
|
c = randint(1, n)
|
|
|
|
while fact == 1:
|
2018-12-16 17:09:54 +08:00
|
|
|
for i in range(cycle_size):
|
2020-04-15 12:28:20 +08:00
|
|
|
if fact > 1:
|
|
|
|
break
|
|
|
|
x = (x*x+c) % n
|
|
|
|
if x == x_fixed:
|
|
|
|
c = randint(1, n)
|
2018-12-16 17:09:54 +08:00
|
|
|
continue
|
2020-04-15 12:28:20 +08:00
|
|
|
fact = gcd(x-x_fixed, n)
|
|
|
|
cycle_size *= 2
|
2018-12-16 17:09:54 +08:00
|
|
|
x_fixed = x
|
|
|
|
return factor(fact)+factor(n//fact)
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
|
2018-12-29 19:30:21 +08:00
|
|
|
def fact(n):
|
2020-04-15 12:28:20 +08:00
|
|
|
f = 2
|
2018-12-29 19:30:21 +08:00
|
|
|
ret = []
|
2020-04-15 12:28:20 +08:00
|
|
|
while f*f <= n:
|
|
|
|
while not n % f:
|
2018-12-29 19:30:21 +08:00
|
|
|
ret.append(f)
|
|
|
|
n//f
|
2020-04-15 12:28:20 +08:00
|
|
|
f += 1
|
|
|
|
if n > 1:
|
|
|
|
ret.append(n)
|
2018-12-29 19:30:21 +08:00
|
|
|
return ret
|
2018-12-16 17:09:54 +08:00
|
|
|
|
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
if __name__ == '__main__':
|
2018-12-16 17:09:54 +08:00
|
|
|
while 1:
|
|
|
|
n = int(input('n: '))
|
|
|
|
print(factor(n))
|