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.9999999999999999999999999999999999999999999999999999999999999999999999999­999999999999999999999999999999999999999999999999999999999999999999999999999­999999999999999999999999999999999999999999999999999999999999999999999999999­999999999999999999999999999999999999999999999999999999999999999999999999923­43

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 =
> 508184298003433059930221143303110332712493139579190463526792062622045893426­23811236647989889145173098650749
> 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
> 126608167051554688363992508839040790329461609432561783112868335758991396849­7538978358203322629420841
> sage: a.is_prime()
> False
> sage: b = a.next_prime(); b
> 897566586864575221876983862371789080887133487597424495265748007237361461447­1639002293590745490978883
> sage: b.is_prime()
> True
>
> --Mike




More information about the Python-list mailing list