Post Reply 
(41) Exponentiation & Residue Reduction
05-08-2020, 01:58 PM
Post: #2
RE: (41) Exponentiation & Residue Reduction
Stack-only, without synthetics, 50 bytes
Calculate R := B^X mod M
Code:
                L       X       Y       Z       T
01>LBL"Z^YMOD"          M       X       B
02 ABS
03 1
04 RDN
05 RDN
06>LBL 02       M       X       B       R
08 2
09 ST/ Y
10 X<> L                M       X/2     B       R
11 X<>Y
12 INT
13 ST- L        FP(X)   IP(X)   M       B       R
14 RDN
15 X<> L
16 X=0?         M       FP(X)   B       R       X
17 GTO 00
18 RDN         @ R := MOD(R*B,M);
19 STx Y
20 X<>Y         M       R.B     B       X
21 LASTX
22 MOD
23 X<> Z        
24>LBL 00       M       -       B       R       X
25 RDN         @ B := MOD(B^2,M);
26 STx X        M       B^2     R       X
27 LASTX
28 MOD          M       B       R       X       X
29 RUP
30 X!=0?        M       X       B       R
31 GTO 02
32 RCL Z
33 END

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (41) Exponentiation & Residue Reduction - Werner - 05-08-2020 01:58 PM



User(s) browsing this thread: 2 Guest(s)