-
Notifications
You must be signed in to change notification settings - Fork 70
/
factor.sage
41 lines (35 loc) · 1.06 KB
/
factor.sage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import sys
# sage verbose
set_verbose(2)
# will take sqrt(n) divisions in the worst case
def trial_division(number):
factors = []
# use different techniques to get primes, dunno which is faster
index = 0
for prime in Primes():
if prime > number:
break
while Mod(number, prime) == 0:
print prime, "divides the order"
factors.append(prime)
number = number // prime
if index == 10000:
print "tested up to prime", prime, "so far"
index = 0
else:
index += 1
return factors
# read stdin
number = int(sys.stdin.read())
print "INPUT NUMBER:", number
# different factorization methods
if sys.argv[1] == "dumb":
print "ALGORITHM: in-house Trial Division"
trial_division(number)
elif sys.argv[1] == "sage":
print "ALGORITHM: Sage factoring algorithm"
print factor(number, verbose=8)
elif sys.argv[1] == "ecm":
print "ALGORITHM: INRIA's ECM"
from sage.libs.libecm import ecmfactor
print ecmfactor(number, 1000000000, verbose=True)