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 / luhn-test-of-credit-card-numbers.wiki

62 lines · 1184 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 tv " valid\n"
8text ti " invalid\n"
9text n1 "49927398716"
10text n2 "49927398717"
11text n3 "1234567812345678"
12text n4 "1234567812345670"
13note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14fn emit a view uchar * to void
15o = ulong 0
16while lt o len a bound 4096
17k = write 1 a o sub len a o
18if le k 0
19exit 1
20end
21o = add o ulong k
22end
23end
24note from the right, odd places add their digit, even places add the digit sum of twice the digit
25fn luhn a view uchar * to bool
26s = ulong 0
27n = len a
28for i ulong n
29d = ulong sub get a sub sub n 1 i 48
30if eq rem i 2 1
31d = mul d 2
32if gt d 9
33d = sub d 9
34end
35end
36s = add s d
37end
38ret eq rem s 10 0
39end
40fn show a view uchar * to void
41emit a
42if luhn a
43emit tv
44else
45emit ti
46end
47end
48fn main to int
49show n1
50show n2
51show n3
52show n4
53ret 0
54end
55</syntaxhighlight>
56{{out}}
57<pre>
5849927398716 valid
5949927398717 invalid
601234567812345678 invalid
611234567812345670 valid
62</pre>