Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

Times[Rational[1,15],Plus[15,Times[-1,Power[-1,n]],Times[-5,Power[2,Plus[2,n]]],Times[3,Power[2,Plus[1,Times[2,n]]]]]]

And to conver all Rationals and Powers to PowerMods manually:

Mod[Times[PowerMod[15,-1,m],Plus[15,Times[-1,PowerMod[-1,n,m]],Times[-5,PowerMod[2,Plus[2,n],m]],Times[3,PowerMod[2,Plus[1,Times[2,n]],m]]]],m]

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?

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

1 Answer 1

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]];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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