Post Reply 
modular exponentiation?
07-09-2018, 05:18 AM
Post: #33
RE: modular exponentiation?
(07-08-2018 09:50 PM)Bill Duncan Wrote:  Has anyone written a POWMOD -like function for the HP-48?

Cf. Modular Arithmetic for the HP-48

Since PLUS is only used by TIMES it can be inlined.
And then you can keep TIMES in a local variable leading to:
Code:
@POWER ( a b n -- a ^ b % n )
« @TIMES ( a b n -- a * b % n )
  « 1000000 → a b n k
    « a k / IP a k MOD
      b k / IP b k MOD
      → x y u v
      « x u * k SQ *
        x v * k *
        y u * k *
        y v *
        4 →LIST
        n MOD
        @PLUS ( a b -- a + b (mod n) )
        « → a b
          « a b +
            IF n OVER ≤
            THEN DROP a n - b +
            END
          »
        »
        STREAM
      »
    »
  »
  → n TIMES
  « 1 ROT ROT
    WHILE DUP
    REPEAT → r a b
      « IF b 2 MOD
        THEN a r n TIMES EVAL @ r → a * r (mod n)
        ELSE r
        END
        a DUP n TIMES EVAL @ a → a ^ 2 (mod n)
        b 2 / IP
      »
    END
    DROP2
  »
»

(07-09-2018 02:32 AM)Joe Horn Wrote:  There's a "Fast Powering algorithm f exponentiation (mod n)" program called PWR in Anthony Shaw's "MODULO" package for the 48: https://www.hpcalc.org/details/6015

Quote:MODPND
Returns a (mod n). This function assumes that a and n have been entered using the # symbol, which allows for large integers (up to about 10^18). By using MODPND in PRODN, numbers whose product do not exceed 10^18 can be multiplied (mod n). If these programs were implemented in machine language, larger integer arithmetic could be made feasible. For me, small numbers have plenty of interesting properties, so I'm not so concerned about this.

#9876543210987654
#1234567890123456 ---> #90000006

Thus I assume that it will work only up to 109.

Quote:Just wondering before I go off and try it myself.

Just go for it! And post your result.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
modular exponentiation? - Bill Duncan - 06-23-2018, 11:49 PM
RE: modular exponentiation? - Thomas Klemm - 06-24-2018, 12:21 AM
RE: modular exponentiation? - Bill Duncan - 06-24-2018, 03:20 AM
RE: modular exponentiation? - Thomas Klemm - 06-24-2018, 12:33 AM
RE: modular exponentiation? - Thomas Okken - 06-24-2018, 12:48 AM
RE: modular exponentiation? - mfleming - 06-24-2018, 02:46 AM
RE: modular exponentiation? - mfleming - 06-24-2018, 02:58 AM
RE: modular exponentiation? - Paul Dale - 06-24-2018, 03:42 AM
RE: modular exponentiation? - J-F Garnier - 06-24-2018, 08:41 AM
RE: modular exponentiation? - John Keith - 06-28-2018, 12:42 AM
RE: modular exponentiation? - ijabbott - 06-28-2018, 06:07 PM
RE: modular exponentiation? - John Keith - 06-28-2018, 09:40 PM
RE: modular exponentiation? - J-F Garnier - 06-24-2018, 11:32 AM
RE: modular exponentiation? - sasa - 06-24-2018, 10:21 PM
RE: modular exponentiation? - ijabbott - 06-25-2018, 10:09 PM
RE: modular exponentiation? - Bill Duncan - 06-25-2018, 10:45 PM
RE: modular exponentiation? - brickviking - 06-25-2018, 02:04 AM
RE: modular exponentiation? - Thomas Okken - 06-25-2018, 02:44 AM
RE: modular exponentiation? - brickviking - 06-25-2018, 10:21 PM
RE: modular exponentiation? - Bill Duncan - 06-25-2018, 02:49 PM
RE: modular exponentiation? - Thomas Okken - 06-25-2018, 03:43 PM
RE: modular exponentiation? - Bill Duncan - 06-25-2018, 04:06 PM
RE: modular exponentiation? - Thomas Klemm - 06-25-2018, 04:31 PM
RE: modular exponentiation? - StephenG1CMZ - 06-25-2018, 05:42 PM
RE: modular exponentiation? - Thomas Klemm - 06-28-2018, 09:56 PM
RE: modular exponentiation? - John Keith - 06-29-2018, 12:47 AM
RE: modular exponentiation? - Bill Duncan - 07-08-2018, 09:50 PM
RE: modular exponentiation? - Joe Horn - 07-09-2018, 02:32 AM
RE: modular exponentiation? - Thomas Klemm - 07-09-2018 05:18 AM
RE: modular exponentiation? - Gerald H - 07-09-2018, 09:55 AM
RE: modular exponentiation? - Thomas Klemm - 07-09-2018, 01:37 PM
RE: modular exponentiation? - wynen - 10-26-2018, 06:08 PM
RE: modular exponentiation? - rprosperi - 10-26-2018, 07:37 PM
RE: modular exponentiation? - Albert Chan - 10-27-2018, 12:24 AM
RE: modular exponentiation? - pier4r - 10-27-2018, 07:28 PM



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