interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / luhn-test-of-credit-card-numbers / luhn-test-of-credit-card-numbers.om

52 lines · 1002 bytes Raw Blame History
1note Rosetta Code task "Luhn test of credit card numbers": https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text tv " valid\n"
6text ti " invalid\n"
7text n1 "49927398716"
8text n2 "49927398717"
9text n3 "1234567812345678"
10text n4 "1234567812345670"
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 from the right, odd places add their digit, even places add the digit sum of twice the digit
23fn luhn a view uchar * to bool
24s = ulong 0
25n = len a
26for i ulong n
27d = ulong sub get a sub sub n 1 i 48
28if eq rem i 2 1
29d = mul d 2
30if gt d 9
31d = sub d 9
32end
33end
34s = add s d
35end
36ret eq rem s 10 0
37end
38fn show a view uchar * to void
39emit a
40if luhn a
41emit tv
42else
43emit ti
44end
45end
46fn main to int
47show n1
48show n2
49show n3
50show n4
51ret 0
52end