interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / modular-exponentiation / modular-exponentiation.om

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