How to factor using Python?
Mensanator
mensanator at aol.com
Tue Mar 11 15:36:04 EDT 2008
On Mar 11, 10:57 am, Mike Hansen <mhan... at gmail.com> wrote:
> 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
If one wants to do serious math using Python, it would be even
better to understand the math before breaking out Sage.
Didn't you realize that cos(pi*n) is a red herring? That you
only need to know if the rational factorial(z-1)/z has a denominator
>1 (although you have to make allowances when it's 2)?
2 Prime: True True Prime: True 1/2
3 Prime: True True Prime: True 2/3
4 Prime: False True Prime: False 3/2
5 Prime: True True Prime: True 24/5
6 Prime: False True Prime: False 20
7 Prime: True True Prime: True 720/7
8 Prime: False True Prime: False 630
9 Prime: False True Prime: False 4480
10 Prime: False True Prime: False 36288
11 Prime: True True Prime: True 3628800/11
12 Prime: False True Prime: False 3326400
13 Prime: True True Prime: True 479001600/13
14 Prime: False True Prime: False 444787200
15 Prime: False True Prime: False 5811886080
16 Prime: False True Prime: False 81729648000
17 Prime: True True Prime: True 20922789888000/17
18 Prime: False True Prime: False 19760412672000
19 Prime: True True Prime: True 6402373705728000/19
>
> 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