Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/modular-exponentiation.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / modular-exponentiation.wiki`
130 lines ยท 2739 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text ta "`2988348162058574136915891421498819466320163312926952423791023078876139`"
8 text tb "`2351399303373464486466122544523690094744975233415544072992656881240319`"
9 text hdr "a^b mod 10^40 = "
10 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11 fn emit a view uchar * to void
12 o = ulong 0
13 while lt o len a bound 4096
14 k = write 1 a o sub len a o
15 if le k 0
16 exit 1
17 end
18 o = add o ulong k
19 end
20 end
21 note emitu writes n in decimal; 20 digits hold every ulong
22 fn emitu n ulong to void
23 d = new uchar 20 0
24 i = ulong 20
25 v = n
26 while true bound 20
27 i = sub i 1
28 put d i uchar add rem v 10 48
29 v = div v 10
30 if eq v 0
31 break
32 end
33 end
34 emit part d i sub 20 i
35 end
36 note a residue mod 10^40 is 5 limbs base 10^8, least significant first
37 note limbs reads the last 40 decimal digits of the 70-digit text s
38 fn limbs s view uchar 70 z edit ulong 5 to void
39 for k ulong 5
40 v = ulong 0
41 o = sub 70 mul 8 add k 1
42 for t ulong 8
43 v = add mul v 10 sub ulong get s add o t 48
44 end
45 put z k v
46 end
47 end
48 note mulm: z = x * y mod 10^40, keeping only limbs 0..4; each column sums at most 5 products below 10^16
49 note plus a carry below 10^9, so no column passes 2^64
50 fn mulm x view ulong 5 y view ulong 5 z edit ulong 5 to void
51 c = ulong 0
52 for k ulong 5
53 s = c
54 for i ulong add k 1
55 s = add s mul get x i get y sub k i
56 end
57 put z k rem s `100000000`
58 c = div s `100000000`
59 end
60 end
61 fn cpy x view ulong 5 z edit ulong 5 to void
62 for k ulong 5
63 put z k get x k
64 end
65 end
66 note bits turns the 70-digit decimal text e into binary, least significant bit first, by repeated halving
67 note bound: e < 10^70 < 2^233, so at most 233 halvings; returns the bit count
68 fn bits e view uchar 70 bt edit uchar 256 to ulong
69 dg = new uchar 70 0
70 for i ulong 70
71 put dg i sub get e i uchar 48
72 end
73 n = ulong 0
74 while true bound 256
75 z = true
76 for i ulong 70
77 if ne get dg i 0
78 z = false
79 end
80 end
81 if z
82 ret n
83 end
84 put bt n and get dg 69 uchar 1
85 r = ulong 0
86 for i ulong 70
87 v = add mul r 10 ulong get dg i
88 put dg i uchar div v 2
89 r = rem v 2
90 end
91 n = add n 1
92 end
93 trap 4
94 end
95 fn main to int
96 a = new ulong 5 0
97 r = new ulong 5 0
98 t = new ulong 5 0
99 u = new ulong 5 0
100 bt = new uchar 256 0
101 limbs ta a
102 put r 0 1
103 n = bits tb bt
104 for i ulong n
105 if ne get bt i 0
106 mulm r a t
107 cpy t r
108 end
109 cpy a u
110 mulm a u t
111 cpy t a
112 end
113 emit hdr
114 emitu get r 4
115 for k ulong 4
116 v = get r sub 3 k
117 d = ulong `10000000`
118 while gt d 0 bound 8
119 emitu rem div v d 10
120 d = div d 10
121 end
122 end
123 emit nl
124 ret 0
125 end
126
127 {{out}}
128
129 a^b mod 10^40 = `1527229998585248450016808958343740453059`
130