# Omnimaga

## Calculator Community => TI Calculators => TI-BASIC => Topic started by: Xeda112358 on January 07, 2013, 11:00:40 am

Title: Optmising BASIC code for A^K mod P
Post by: Xeda112358 on January 07, 2013, 11:00:40 am
I made this code to compute AK mod P and I was wondering if it could be optmised and still preserve the speediness (or make it faster).
Code: [Select]
`:1→M:While int(K:.5K→K:If int(2fPart(Ans:AM-Pint(AM/P→M:A²-Pint(A²/P→A:End`I feel like it could be optimised in BASIC. And maybe use only three variables (base, exponent, mod).
EDIT: Optimised with suggestions from calc84maniac and Jacobly
EDIT2: Figured out that .5K is faster than K/2
Title: Re: Optmising BASIC code for A^K mod P
Post by: jacobly on January 07, 2013, 12:24:58 pm
I have no idea if this is faster, but it doesn't use M.

Code: [Select]
`{.5K,A,1While int(2Ans(1Ans{.5,Ans(2),1+(Ans(2)-1)int(2fPart(Ans(1Ans-Pint({0,1,1}Ans/PEndAns(3`
Title: Re: Optmising BASIC code for A^K mod P
Post by: Xeda112358 on January 07, 2013, 12:31:07 pm
Wow, it isn't faster, but it is still pretty fast and only a few byte bigger than the code I had (excluding the first and last line). That is an awesome use of lists!
Title: Re: Optmising BASIC code for A^K mod P
Post by: Deep Toaster on January 07, 2013, 02:05:09 pm
`1→MWhile iPart(K.5K→KIf int(2fPart(AnsPfPart(AM/P→MPfPart(A2/P→AEnd`
`1For(I,1,KPfPart(AAns/PEndround(Ans,0`