interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / isqrt-integer-square-root-of-x.wiki

93 lines · 1965 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text small "isqrt of 0..65:"
9text pre "isqrt(7^"
10text eqs ") = "
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note the task's quadratic residue method: q is the least power of 4 above x, then one result bit per step
38note bound: inputs here are below 2^60, so q stays at most 2^60 (30 doublings of the exponent) and never wraps;
39note 7^n is shown for odd n up to 21, the largest odd power of 7 below 2^64 (the task goes on to 7^73 with bignums)
40fn isqrt x ulong to ulong
41q = ulong 1
42while le q x bound 31
43q = shl q 2
44end
45z = x
46r = ulong 0
47while gt q 1 bound 31
48q = shr q 2
49s = add r q
50r = shr r 1
51if ge z s
52z = sub z s
53r = add r q
54end
55end
56ret r
57end
58fn main to int
59emit small
60for x ulong 66
61emit sp
62emitu isqrt x
63end
64emit nl
65p = ulong 7
66n = ulong 1
67while le n 21 bound 11
68emit pre
69emitu n
70emit eqs
71emitu isqrt p
72emit nl
73p = mul p 49
74n = add n 2
75end
76ret 0
77end
78</syntaxhighlight>
79{{out}}
80<pre>
81isqrt of 0..65: 0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
82isqrt(7^1) = 2
83isqrt(7^3) = 18
84isqrt(7^5) = 129
85isqrt(7^7) = 907
86isqrt(7^9) = 6352
87isqrt(7^11) = 44467
88isqrt(7^13) = 311269
89isqrt(7^15) = 2178889
90isqrt(7^17) = 15252229
91isqrt(7^19) = 106765608
92isqrt(7^21) = 747359260
93</pre>