How to factor using Python?

Mike Hansen mhansen at gmail.com
Tue Mar 11 16:57:29 CET 2008


If one wants to do serious math using Python, the best bet is to use
Sage ( http://www.sagemath.org ).  Here are some examples:

sage: def f(x, bits=53):
....:     R = RealField(bits); z = R(x)
....:     return cos(R(pi) * factorial(z-1) / z)
sage: f(100.00,bits=1000)
0.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999992343

sage: a =
50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749
sage: time ecm.factor(a)
CPU times: user 0.00 s, sys: 0.06 s, total: 0.06 s
Wall time: 2.63

[3478697,
 49998841,
 11927295803,
 518069464441,
 1858900129817,
 161610704597143,
 157394131396743433859615518992811454816816449]

sage: a = ZZ.random_element(10**100); a
1266081670515546883639925088390407903294616094325617831128683357589913968497538978358203322629420841
sage: a.is_prime()
False
sage: b = a.next_prime(); b
8975665868645752218769838623717890808871334875974244952657480072373614614471639002293590745490978883
sage: b.is_prime()
True

--Mike



More information about the Python-list mailing list