modulus



> calculation ... 489^2281 mod 3293
> ... I have tried factor(489^2281),modulus:3293;
> ... I can now do 2*13*43
>But how can I get this result directly in a one-line command?

Recall that the "x,y" command-line syntax is precisely equivalent to
"ev(x,y)".  So you could write

   expand(ev(factor(489^2281),modulus:3293))

But all "modulus:3293" does is bind modulus temporarily to 3293.  So you
could equally well write:

   ev(expand(factor(489^2281)),modulus:3293);

... and in short form...

   expand(factor(489^2281)),modulus:3293;

But of course you really don't need to factor at all.  So what you
really should be doing is:

   rat(489)^2281,modulus:3293

Explanation:

The setting of "modulus" only affects calculations in the rational
function package.  Luckily for you, "factor" happens to use that
package, but that is going to be slow since you don't really need to
factor.

Now, why not calculate rat(489^2281) with modulus=3293?  In that case,
Maxima will first calculate 489^2281 as an integer (all 6000+ digits),
then divide by 3293 and return the remainder.    Though multiprecision
integer arithmetic is pretty fast, it does take a lot of space, and you
will probably be limited by running out of memory.  (Actually it is
worse than that -- in the GCL version, there is a bug where huge integer
calculations corrupt memory.)

If on the other hand you calculate rat(489)^2281 with modulus=3293, the
exponentiation is carried out in modular arithmetic, so intermediate
results never get larger than 3292 (actually Maxima uses a balanced
notation, so the range is -1646 to 1646).

Example:

  modulus: 3293     /* set modulus globally */ 
  showtime: true    /* show execution times */ 
  expand(factor(234^234234))
     0.70 seconds
          -924
  rat(234^234234)
     0.62 seconds
     /R/  -924
  rat(234)^234234
     0.00 seconds
     /R/  -924

  modulus:10000004400000259$
  expand(factor(rat(234)^234239))
  10.45 seconds
  rat(234)^234239;
  Evaluation took 0.00 seconds
  rat(2342347)^234239;
  Evaluation took 0.00 seconds
  rat(2342347^234239)
  Error: Caught fatal error [memory may be damaged]

Hope this helps.

       -s

PS Careful: Don't use rat(xxx)^rat(yyy): if yyy>modulus/2, the exponent
will be negative!  For example: modulus:7, rat(3)=>3, rat(5)=>-2,
3^5=>243, rat(3)^5=>-2, rat(3)^rat(5)=>-3 (because 3^-2=1/(3^2)=1/2=-3
(mod 5)), and ...surprise!... 3^rat(5)=>1/9 (rational expressions in the
exponent aren't contagious).