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 / magic-squares-of-odd-order.wiki

122 lines · 2425 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 minus "-"
9text tconst "magic constant: "
10global grid ulong 81
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 emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut)
38fn emitw n ulong w ulong to void
39d = new uchar 20 32
40i = ulong 20
41v = n
42while true bound 20
43i = sub i 1
44put d i uchar add rem v 10 48
45v = div v 10
46if eq v 0
47break
48end
49end
50if gt w sub 20 i
51i = sub 20 w
52end
53emit part d i sub 20 i
54end
55note the Siamese method: 1 goes in the middle of the top row; each next number goes one up and one
56note right (wrapping round), or straight down when that cell is taken, which happens every n numbers
57fn siamese n ulong to void
58for i ulong mul n n
59put grid i 0
60end
61r = ulong 0
62c = div n 2
63for k ulong mul n n
64put grid add mul r n c add k 1
65ur = rem sub add r n 1 n
66uc = rem add c 1 n
67if eq get grid add mul ur n uc 0
68r = ur
69c = uc
70else
71r = rem add r 1 n
72end
73end
74end
75fn show n ulong to void
76for i ulong n
77for j ulong n
78emitw get grid add mul i n j 4
79end
80emit nl
81end
82emit tconst
83emitu div mul n add mul n n 1 2
84emit nl
85end
86fn main to int
87siamese 3
88show 3
89emit nl
90siamese 5
91show 5
92emit nl
93siamese 9
94show 9
95ret 0
96end
97</syntaxhighlight>
98{{out}}
99<pre>
100 8 1 6
101 3 5 7
102 4 9 2
103magic constant: 15
104
105 17 24 1 8 15
106 23 5 7 14 16
107 4 6 13 20 22
108 10 12 19 21 3
109 11 18 25 2 9
110magic constant: 65
111
112 47 58 69 80 1 12 23 34 45
113 57 68 79 9 11 22 33 44 46
114 67 78 8 10 21 32 43 54 56
115 77 7 18 20 31 42 53 55 66
116 6 17 19 30 41 52 63 65 76
117 16 27 29 40 51 62 64 75 5
118 26 28 39 50 61 72 74 4 15
119 36 38 49 60 71 73 3 14 25
120 37 48 59 70 81 2 13 24 35
121magic constant: 369
122</pre>