(32SII) Solving a Single Congruence Equation for the HP 32Sii
|
01-16-2014, 03:16 AM
(This post was last modified: 06-15-2017 01:30 PM by Gene.)
Post: #1
|
|||
|
|||
(32SII) Solving a Single Congruence Equation for the HP 32Sii
The program solves for x in the equation:
A * x = B mod N Examples: 4 * x = 6 mod 7 A = 4, B = 6, N = 7 Solution: 5 5 * x = 3 mod 17 A = 5, B = 3, N = 17 Solution: 4 11 * x = 3 mod 16 A = 11, B = 3, N = 16 Solution: 9 Main Routine: Label C Subroutines: D, E No pre-storing any variables required. If there is no solution, an error is produced. To Run: XEQ C Code:
|
|||
01-16-2014, 05:58 AM
(This post was last modified: 01-16-2014 06:20 AM by Thomas Klemm.)
Post: #2
|
|||
|
|||
RE: Solving a Single Congruence Equation for the HP 32Sii
Instead of brute force the Chinese remainder theorem could be used. This program for the HP-11C can easily be translated to the HP-32Sii or most other HP calculators.
001 LBL A 002 STO 1 003 STO 5 004 R↓ 005 STO 6 006 R↓ 007 STO 2 008 0 009 STO 3 010 1 011 STO 4 012 LBL 0 013 RCL 1 014 RCL 2 015 / 016 INT 017 STO 0 018 RCL 3 019 RCL 4 020 STO 3 021 RCL 0 022 * 023 - 024 STO 4 025 RCL 1 026 RCL 2 027 STO 1 028 RCL 0 029 * 030 - 031 STO 2 032 X≠0 033 GTO 0 034 RCL 5 035 RCL 3 036 X<0 037 + 038 RCL 6 039 * 040 ENTER 041 ENTER 042 RCL 5 043 / 044 INT 045 RCL 5 046 * 047 - 048 RTN Usage: A ENTER B ENTER N GSB A Example: 5 * x = 3 mod 17 5 ENTER 3 ENTER 17 GSB A 4 Restriction: gcd(A, N) = 1 |
|||
01-17-2014, 09:04 PM
Post: #3
|
|||
|
|||
RE: Solving a Single Congruence Equation for the HP 32Sii
This is the correspoding Python code:
Code:
Visualize Execution Now how cool is that? Imagine you write the program for your calculator and view the stack and registers? Single-step forward and back through the program? Cheers Thomas |
|||
03-06-2019, 04:50 AM
(This post was last modified: 03-10-2019 08:18 PM by Albert Chan.)
Post: #4
|
|||
|
|||
RE: (32SII) Solving a Single Congruence Equation for the HP 32Sii
This version handle cases when a (mod n) has no inverse
Code: def LC(a,b,n): >>> LC(5, 3, 17) # 1 solution (4, 17) >>> LC(123,320,777) # no solution () >>> LC(123,321,777) # 3 solutions (mod 777) (110, 259) >>> [ 123*x % 777 for x in range(110,777,259)] [321, 321, 321] |
|||
03-10-2019, 07:41 PM
(This post was last modified: 08-23-2019 01:18 AM by Albert Chan.)
Post: #5
|
|||
|
|||
RE: (32SII) Solving a Single Congruence Equation for the HP 32Sii
With post #4 LC(), for solving A x ≡ B (mod N), we can do Simultaneuous Linear Congruence
Code: def SLC(eqn1, eqn2): Example: x≡3 (mod 7) AND x≡105 (mod 143) AND x≡27 (mod 312) AND x≡248 (mod 715) >>> reduce(SLC, [(3,7), (105, 143), (27, 312), (248, 715)]) (35283, 120120) >>> # Second congruence is covered by the fourth, and safe to eliminate ... >>> reduce(SLC, [(3,7), (27, 312), (248, 715)]) (35283, 120120) |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)