omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / modular-exponentiation / modular-exponentiation.om
| 1 | note Rosetta Code task "Modular exponentiation": https://rosettacode.org/wiki/Modular_exponentiation |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text ta "2988348162058574136915891421498819466320163312926952423791023078876139" |
| 6 | text tb "2351399303373464486466122544523690094744975233415544072992656881240319" |
| 7 | text hdr "a^b mod 10^40 = " |
| 8 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 9 | fn emit a view uchar * to void |
| 10 | o = ulong 0 |
| 11 | while lt o len a bound 4096 |
| 12 | k = write 1 a o sub len a o |
| 13 | if le k 0 |
| 14 | exit 1 |
| 15 | end |
| 16 | o = add o ulong k |
| 17 | end |
| 18 | end |
| 19 | note emitu writes n in decimal; 20 digits hold every ulong |
| 20 | fn emitu n ulong to void |
| 21 | d = new uchar 20 0 |
| 22 | i = ulong 20 |
| 23 | v = n |
| 24 | while true bound 20 |
| 25 | i = sub i 1 |
| 26 | put d i uchar add rem v 10 48 |
| 27 | v = div v 10 |
| 28 | if eq v 0 |
| 29 | break |
| 30 | end |
| 31 | end |
| 32 | emit part d i sub 20 i |
| 33 | end |
| 34 | note a residue mod 10^40 is 5 limbs base 10^8, least significant first |
| 35 | note limbs reads the last 40 decimal digits of the 70-digit text s |
| 36 | fn limbs s view uchar 70 z edit ulong 5 to void |
| 37 | for k ulong 5 |
| 38 | v = ulong 0 |
| 39 | o = sub 70 mul 8 add k 1 |
| 40 | for t ulong 8 |
| 41 | v = add mul v 10 sub ulong get s add o t 48 |
| 42 | end |
| 43 | put z k v |
| 44 | end |
| 45 | end |
| 46 | note mulm: z = x * y mod 10^40, keeping only limbs 0..4; each column sums at most 5 products below 10^16 |
| 47 | note plus a carry below 10^9, so no column passes 2^64 |
| 48 | fn mulm x view ulong 5 y view ulong 5 z edit ulong 5 to void |
| 49 | c = ulong 0 |
| 50 | for k ulong 5 |
| 51 | s = c |
| 52 | for i ulong add k 1 |
| 53 | s = add s mul get x i get y sub k i |
| 54 | end |
| 55 | put z k rem s 100000000 |
| 56 | c = div s 100000000 |
| 57 | end |
| 58 | end |
| 59 | fn cpy x view ulong 5 z edit ulong 5 to void |
| 60 | for k ulong 5 |
| 61 | put z k get x k |
| 62 | end |
| 63 | end |
| 64 | note bits turns the 70-digit decimal text e into binary, least significant bit first, by repeated halving |
| 65 | note bound: e < 10^70 < 2^233, so at most 233 halvings; returns the bit count |
| 66 | fn bits e view uchar 70 bt edit uchar 256 to ulong |
| 67 | dg = new uchar 70 0 |
| 68 | for i ulong 70 |
| 69 | put dg i sub get e i uchar 48 |
| 70 | end |
| 71 | n = ulong 0 |
| 72 | while true bound 256 |
| 73 | z = true |
| 74 | for i ulong 70 |
| 75 | if ne get dg i 0 |
| 76 | z = false |
| 77 | end |
| 78 | end |
| 79 | if z |
| 80 | ret n |
| 81 | end |
| 82 | put bt n and get dg 69 uchar 1 |
| 83 | r = ulong 0 |
| 84 | for i ulong 70 |
| 85 | v = add mul r 10 ulong get dg i |
| 86 | put dg i uchar div v 2 |
| 87 | r = rem v 2 |
| 88 | end |
| 89 | n = add n 1 |
| 90 | end |
| 91 | trap 4 |
| 92 | end |
| 93 | fn main to int |
| 94 | a = new ulong 5 0 |
| 95 | r = new ulong 5 0 |
| 96 | t = new ulong 5 0 |
| 97 | u = new ulong 5 0 |
| 98 | bt = new uchar 256 0 |
| 99 | limbs ta a |
| 100 | put r 0 1 |
| 101 | n = bits tb bt |
| 102 | for i ulong n |
| 103 | if ne get bt i 0 |
| 104 | mulm r a t |
| 105 | cpy t r |
| 106 | end |
| 107 | cpy a u |
| 108 | mulm a u t |
| 109 | cpy t a |
| 110 | end |
| 111 | emit hdr |
| 112 | emitu get r 4 |
| 113 | for k ulong 4 |
| 114 | v = get r sub 3 k |
| 115 | d = ulong 10000000 |
| 116 | while gt d 0 bound 8 |
| 117 | emitu rem div v d 10 |
| 118 | d = div d 10 |
| 119 | end |
| 120 | end |
| 121 | emit nl |
| 122 | ret 0 |
| 123 | end |