I have the expression $\frac{1}{15} \left(-(-1)^n-5\times 2^{n+2}+3\times 2^{2 n+1}+15\right)$ which I want to calculate mod m for a very large n.

My current method is to ask for this exression in FullForm:


And to conver all Rationals and Powers to PowerMods manually:


However, this is kind of time-consuming and I'm sure there's a better way. Is there any sort of 'smart' find-and-replace function I can apply to do something like this?

  • $\begingroup$ m has about 8 digits and n has about $10^{18}$ digits. $\endgroup$ – Jakob Weisblat May 10 '14 at 15:34
  • $\begingroup$ But n can be reduced mod $\lambda(n)$ $\endgroup$ – Jakob Weisblat May 10 '14 at 15:37
  • $\begingroup$ @Kuba I just found it and wrote an answer to my own question using it, right before I saw your comment. :P $\endgroup$ – Jakob Weisblat May 10 '14 at 15:38

Yes, use a few auxiliary functions and ReplaceAll:

power = PowerMod[#, #2, m] &;

fractionMod = Mod[Numerator[#]*PowerMod[Denominator[#], -1, m], m] &;

newThing = fractionMod[ReplaceAll[oldThing, Power -> power]];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.