(41) Exponentiation & Residue Reduction
|
04-06-2020, 01:31 PM
(This post was last modified: 04-26-2022 04:13 PM by SlideRule.)
Post: #1
|
|||
|
|||
(41) Exponentiation & Residue Reduction
Pages 341 & 344 from Number Theory in Science and Communication, Second Enlarged Edition, © Sprinler·Veriag Berlin Heidelberg 1984 and 1986, ISBN 978-3-662-22246-1 (eBoook)
"A. A Calculator Program for Exponentiation and Residue Reduction We list here a program for calculating the least nonnegative remainder of aⁿ modulo m on a Hewlett-Packard 41C or 41CV pocket calculator. In other words, in our acute bracket notation, the calculator calculates The 3 variables, the base a, the exponent n and the modulus m are entered into the calculator in that order. To call the program, which is labeled "AN", from storage, one presses GTO "AN" . To calculate, say, {2³⁴⁰}₃₄₁ , one proceeds by pressing the following buttons: 2 ENTER 340 ENTER 341 To start the program, one presses R/S After 2 seconds the HP 41 display shows in rapid succession 8. 6. 4. 2. This is the binary decomposition of the exponent 340. Check: 28 + 26 + 24 + 22 = 340. Check! Next, the computer will start the necessary repeated squarings and reductions modulo 341, which will take about 7 seconds. Then the display will show the end result, a 1 (without a decimal point!). Thus, {2³⁴⁰)₃₄₁ = 1 . In other words, 341 is a pseudoprime to the base 2. Similarly, after pressing 2, ENTER, 170, ENTER, 341, R/S, the display shows in succession: 7. 5. 3. 1. 1 where that last display (the 1 without the decimal point) tells us that {2¹⁷⁰}₃₄₁ = 1 . Proceeding in the same manner, we find {2⁸⁵}₃₄₁ = 32 . i.e., 341 is not a strong pseudoprime to the base 2. Also, by using 3 as a base we find {3³⁴⁰}₃₄₁ = 56 . Thus, 341 is certainly not an absolute pseudoprime (Carmichael number). However, for the modulus 2821, we find {2²⁸²⁰}₂₈₂₁ = {3²⁸²⁰}₂₈₂₁ = 1 . two of the many steps necessary to show that 2821 is an absolute pseudoprime or a Carmichael number Note that our little calculator with a limited accuracy and a 10-digit display, in calculating (3²⁸²⁰}₂₈₂₁, has coped with a number having 1346 decimal digits! This has been made possible by the frequent intermediate modulo reduction that the program employs. Listing for "AN" Comment Step Code call program 01 LBL "AN" decimal point 02 SF 29 store modulus 03 STO 18 get exponent 04 RDN store exponent 05 STO 17 get base 06 RDN store base 07 STO 16 constant 08 2 store 2 09 STO 01 take logarithm 10 LN store log 11 STO 15 display no fractions 12 FIX 0 constant 13 1 store 1 14 STO 14 constant 15 0 store 0 16 STO 02 subroutine for calculating 17 LBL 10 binary representation of n 18 RCL 17 19 LN 20 RCL 15 21 / add small constant to 22 0.000000001 avoid inaccurate rounding 23 + 24 INT 25 ENTER 26 ST- IND 01 display the binary 27 PSE exponents of n 28 1 29 ST+ 01 30 RDN 31 STO IND 01 32 2 33 x <> y 34 y^x 35 ST- 17 36 RCL 17 binary representation 37 x = 0? of n completed? 38 GTO 11 39 GTO 10 subroutine for executing 40 LBL 11 repeated squaring 41 RCL IND 01 42 x = 0? 43 GTO 12 44 RCL16 45 RCL 18 46 2 47 / 48 x < > y 49 x > y? 50 XEQ 16 51 x² 52 RCL 18 take remainder 53 MOD modulo n 54 STO 16 55 1 56 ST- IND 01 57 GTO 11 subroutine for cal- 58 LBL 12 culating intermediate 59 RCL 16 squaring results 60 ST* 14 61 RCL 14 62 RCL 18 63 MOD 64 STO 14 65 1 66 ST- 01 67 RCL 01 68 2 69 x = y? 70 GTO 13 71 GTO 11 72 LBL 16 73 RCL 18 74 - 75 RTN Subroutine for 76 LBL 13 recalling and 77 RCL 14 displaying residue 78 CF 29 display residue 79 STOP ready to start over 80 GTO "AN" 81 RTN 82 END" BEST! SlideRule |
|||
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 Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
05-11-2020, 02:55 PM
(This post was last modified: 05-11-2020 07:30 PM by Albert Chan.)
Post: #3
|
|||
|
|||
RE: (41) Exponentiation & Residue Reduction
(05-08-2020 01:58 PM)Werner Wrote: Stack-only, without synthetics, 50 bytes Amazing ! I touch up a bit so that it can copy/paste directly into Free42 (52 bytes) Code: 01▸LBL "Z↑YMOD" Example 123^456 mod 789: 123 [Enter] 456 [Enter] 789 [XEQ] "Z↑YMOD" → 699 This version does binary ladder expoentiation from right to left, like this: Code: (define (pow-mod b x m r) scheme> (trace pow-mod) (pow-mod) scheme> (pow-mod 123 456 789 1) |(pow-mod 123 456 789 1) |(pow-mod 138 228 789 1) |(pow-mod 108 114 789 1) |(pow-mod 618 57 789 1) |(pow-mod 48 28 789 618) |(pow-mod 726 14 789 618) |(pow-mod 24 7 789 618) |(pow-mod 576 3 789 630) |(pow-mod 396 1 789 729) |(pow-mod 594 0 789 699) |699 699 |
|||
04-26-2022, 03:17 AM
(This post was last modified: 04-26-2022 06:10 AM by Thomas Klemm.)
Post: #4
|
|||
|
|||
RE: (41) Exponentiation & Residue Reduction
Let us start with the following Python program:
Code: def pow(b, x, m): With the help of the Python to RPN - source code converter it is translated to: Code: LBL "Z^Y%X" // Label. Identify programs and routines for execution and branching. Parameter: local or global label. Let's clear it up a bit:
Instead of: Code: X≠0? // while true? Code: X=0? // while not? With these changes we are already down at 51 bytes and could call it a day: Code: 00 { 51-Byte Prgm } But can we do better? You may have noticed that line 13 could be removed since x is still present from line 10. Furthermore, division by 2 and modulo 2 of x can be combined. Code: 00 { 47-Byte Prgm } So far we haven't bothered with registers or keeping results on the stack. But if we leave x on the stack, we save 2 STO and 1 RCL instructions at the cost of 2 RDN instructions. With this we not only save a register, but also another byte: Code: 00 { 46-Byte Prgm } Feel free to replace the 3 remaining registers with the synthetic registers M, N and O. |
|||
04-26-2022, 10:59 AM
(This post was last modified: 04-26-2022 01:20 PM by Thomas Klemm.)
Post: #5
|
|||
|
|||
Errata: (41) Exponentiation & Residue Reduction
Errata
342 Appendix This is the binary decomposition of the exponent 340. Check: \(2^8 + 2^6 + 2^4 + 2^2 = 340\). Check! Next, the computer will start the necessary repeated squarings and reductions modulo 341, which will take about 7 seconds. Then the display will show the end result, a 1 (without a decimal point!). Thus, \( \left< 2^{340} \right>_{341} = 1 \). In other words, 341 is a pseudoprime to the base 2. Similarly, after pressing 2, ENTER, 170, ENTER, 341, R/S, the display shows in succession: 7. 5. 3. 1. 1 where that last display (the 1 without the decimal point) tells us that \( \left< 2^{170} \right>_{341} = 1 \). Proceeding in the same manner, we find \( \left< 2^{85} \right>_{341} = 32 \), i.e., 341 is not a strong pseudoprime to the base 2. Also, by using 3 as a base we find \( \left< 3^{340} \right>_{341} = 56 \). Thus, 341 is certainly not an absolute pseudoprime (Carmichael number). However, for the modulus 2821, we find \( \left< 2^{2820} \right>_{2821} = \left< 3^{2820} \right>_{2821} = 1 \), two of the many steps necessary to show that 2821 is an absolute pseudoprime or a Carmichael number. Note that our little calculator with a limited accuracy and a 10-digit display, in calculating \(\left< 2^{2820} \right>_{2821}\) has coped with a number having 1346 decimal digits! This has been made possible by the frequent intermediate modulo reduction that the program employs. Appendix 343 Original program for the HP-41C: Code: 01 LBL "AN" ; call program Translated program for the HP-42S: Code: 00 { 141-Byte Prgm } Resources |
|||
04-26-2022, 04:05 PM
(This post was last modified: 04-26-2022 04:14 PM by Thomas Klemm.)
Post: #6
|
|||
|
|||
Errata: (41) Exponentiation & Residue Reduction
(04-06-2020 01:31 PM)SlideRule Wrote: Proceeding in the same manner, we findThat sentence didn't make any sense to me, so I looked it up in the original: (04-26-2022 10:59 AM)Thomas Klemm Wrote: Proceeding in the same manner, we findThis makes a lot more sense. Most of the original PDF can be copied, but among other things, the formula from above is missing and so had to be pasted in manually by the OP. I hope these fixes will make the article easier to read and also allow you to run the original program. For those interested in the topic: |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 6 Guest(s)