Damm algorithm: Difference between revisions

Add SETL
(Add SETL)
(47 intermediate revisions by 29 users not shown)
Line 15:
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight lang="11l">V matrix = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
Line 35:
 
L(test) [5724, 5727, 112946]
print(test"\t Validates as: "damm(test))</langsyntaxhighlight>
{{out}}
<pre>
Line 42:
112946 Validates as: 1B
</pre>
 
 
=={{header|8080 Assembly}}==
 
<langsyntaxhighlight lang="8080asm"> org 100h
jmp demo
;;; Given an 0-terminated ASCII string containing digits in [DE],
Line 98 ⟶ 99:
jmp 5
no: db 'NOT '
ok: db 'OK$'</langsyntaxhighlight>
 
{{out}}
Line 113 ⟶ 114:
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm"> cpu 8086
bits 16
section .text
Line 158 ⟶ 159:
section .data
no: db 'NOT '
ok: db 'OK$'</langsyntaxhighlight>
 
{{out}}
Line 171 ⟶ 172:
NOT OK</pre>
 
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE FUNC Damm(CHAR ARRAY a)
BYTE ARRAY table=[
0 3 1 7 5 9 8 6 4 2
7 0 9 2 1 5 4 8 6 3
4 2 0 6 8 7 1 3 5 9
1 7 5 0 9 8 3 4 2 6
6 1 2 3 0 4 5 9 7 8
3 6 7 4 2 0 9 5 8 1
5 8 6 9 7 2 0 1 3 4
8 9 4 5 3 6 2 0 1 7
9 4 3 8 6 1 7 2 0 5
2 5 8 1 4 3 6 7 9 0]
BYTE i,x,c
 
x=0
FOR i=1 TO a(0)
DO
c=a(i)
IF c<'0 OR c>'9 THEN
RETURN (0)
FI
c==-'0
x=table(x*10+c)
OD
IF x=0 THEN
RETURN (1)
FI
RETURN (0)
 
PROC Test(CHAR ARRAY a)
BYTE i
 
Print(a) Print(" -> ")
IF Damm(a)=1 THEN
PrintE("valid")
ELSE
PrintE("invalid")
FI
RETURN
 
PROC Main()
Test("5724")
Test("5727")
Test("112946")
Test("112949")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Damm_algorithm.png Screenshot from Atari 8-bit computer]
<pre>
5724 -> valid
5727 -> invalid
112946 -> valid
112949 -> invalid
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Damm_Algorithm is
Line 211 ⟶ 268:
Put_Damm ("112946");
Put_Damm ("112949");
end Damm_Algorithm;</langsyntaxhighlight>
 
{{out}}
Line 221 ⟶ 278:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN
# returns TRUE if the check digit of s is correct according to the Damm algorithm, #
# FALSE otherwise #
Line 271 ⟶ 328:
test damm algorithm( "5727", FALSE );
test damm algorithm( "112946", TRUE )
END</langsyntaxhighlight>
{{out}}
<pre>check digit of 5724 is valid
Line 282 ⟶ 339:
This is a function that takes a vector of digits and returns a boolean.
 
<langsyntaxhighlight lang="apl"> damm←{⎕IO←0
tbl←⍉⍪0 3 1 7 5 9 8 6 4 2
tbl⍪← 7 0 9 2 1 5 4 8 6 3
Line 294 ⟶ 351:
tbl⍪← 2 5 8 1 4 3 6 7 9 0
0={tbl[⍵;⍺]}/⌽0,⍵
}</langsyntaxhighlight>
 
{{out}}
Line 309 ⟶ 366:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">-- Return a check digit value for the given integer value or numeric string.
-- The result is 0 if the input's last digit is already a valid check digit for it.
on damm(n)
Line 349 ⟶ 406:
set end of output to (n as text) & item (((damm(n) is 0) as integer) + 1) of possibilities
end repeat
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{"5724 is valid", "57240 is valid", "572400 is valid", "87591 is invalid", "100 is invalid", "3922446 is valid"}</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
<syntaxhighlight lang="text">.text
.global _start
@@@ Check if the zero-terminated ASCII string in [r0],
Line 404 ⟶ 461:
swi #0
pass: .ascii "Pass\n"
fail: .ascii "Fail\n"</langsyntaxhighlight>
 
{{out}}
Line 416 ⟶ 473:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">; by @Krenium
 
table: [
Line 431 ⟶ 488:
]
 
digitsdamm?: function [xz][mapzero? splitfold xdigits => [to :integer z .seed: 0 [x y]-> table\[x]\[y] ]
 
damm?: function [z][zero? fold digits z .seed: 0 => [get get table]]
; Or, being more explicit:
digits2: function [str][
chars: split str
result: map chars 'ch -> to :integer ch
return result
]
 
damm2?: function [str][
d: digits2 str
r: fold d .seed: 0 [x y] -> get get table x y
return r = 0
]
 
test: function [str][
Line 440 ⟶ 509:
]
 
loop ["5724" "5727" "112946" "112949"] => test</langsyntaxhighlight>
 
{{out}}
Line 450 ⟶ 519:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Damm(num){
row := 1, Damm := [[0,3,1,7,5,9,8,6,4,2]
,[7,0,9,2,1,5,4,8,6,3]
Line 465 ⟶ 534:
}
return (SubStr(num, 0)=row-1 && !Damm[row, row])
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">result := ""
for i, num in [5724, 5727, 112946, 112949]
result .= num "`tis " (Damm(num) ? "valid" : "not valid") "`n"
MsgBox % result</langsyntaxhighlight>
Outputs:<pre>5724 is valid
5727 is not valid
Line 476 ⟶ 545:
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk"># syntax: GAWK -f DAMM_ALGORITHM.AWK
BEGIN {
damm_init()
Line 505 ⟶ 574:
damm[8] = "9438617205"
damm[9] = "2581436790"
}</langsyntaxhighlight>
{{out}}
<pre>T 5724
Line 512 ⟶ 581:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|GW-BASIC}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 REM Damm algorithm
110 OPTION BASE 0
120 DIM DT(9, 9)
130 FOR Y = 0 TO 9
140 FOR X = 0 TO 9
150 READ DT(X, Y)
160 NEXT X
170 NEXT Y
180 INPUT N$
190 DO WHILE N$ <> ""
200 LET D = 0
210 FOR I = 1 TO LEN(N$)
220 LET D = DT(VAL(MID$(N$, I, 1)), D)
230 NEXT I
240 IF D <> 0 THEN PRINT "FAIL" ELSE PRINT "PASS"
250 INPUT N$
260 LOOP
270 DATA 0, 3, 1, 7, 5, 9, 8, 6, 4, 2
280 DATA 7, 0, 9, 2, 1, 5, 4, 8, 6, 3
290 DATA 4, 2, 0, 6, 8, 7, 1, 3, 5, 9
300 DATA 1, 7, 5, 0, 9, 8, 3, 4, 2, 6
310 DATA 6, 1, 2, 3, 0, 4, 5, 9, 7, 8
320 DATA 3, 6, 7, 4, 2, 0, 9, 5, 8, 1
330 DATA 5, 8, 6, 9, 7, 2, 0, 1, 3, 4
340 DATA 8, 9, 4, 5, 3, 6, 2, 0, 1, 7
350 DATA 9, 4, 3, 8, 6, 1, 7, 2, 0, 5
360 DATA 2, 5, 8, 1, 4, 3, 6, 7, 9, 0
370 END
</syntaxhighlight>
{{out}}
<pre>
<pre>
? 5724
PASS
? 5727
FAIL
? 112946
PASS
? 112949
FAIL
?
</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">arraybase 1
global matrix
matrix = {{0, 3, 1, 7, 5, 9, 8, 6, 4, 2}, {7, 0, 9, 2, 1, 5, 4, 8, 6, 3}, {4, 2, 0, 6, 8, 7, 1, 3, 5, 9}, {1, 7, 5, 0, 9, 8, 3, 4, 2, 6}, {6, 1, 2, 3, 0, 4, 5, 9, 7, 8}, {3, 6, 7, 4, 2, 0, 9, 5, 8, 1}, {5, 8, 6, 9, 7, 2, 0, 1, 3, 4}, {8, 9, 4, 5, 3, 6, 2, 0, 1, 7}, {9, 4, 3, 8, 6, 1, 7, 2, 0, 5}, {2, 5, 8, 1, 4, 3, 6, 7, 9, 0}}
test = {5724, 5727, 112946}
 
for i = 1 to 3
print "Checksum test: "; rjust(string(test[i]),8); encode(test[i])
next i
end
 
function encode(n)
cad = string(n)
check = 0
for d = 1 to length(cad)
check = matrix[int(mid(cad, d, 1)), d]
next d
if check = 0 then
return " is valid"
else
return " is invalid"
end if
end function</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' version 04-07-2018
' compile with: fbc -s console
 
Function Damm(digit_str As String) As UInteger
 
Dim As UInteger table(10,10) => { { 0, 3, 1, 7, 5, 9, 8, 6, 4, 2 } , _
{ 7, 0, 9, 2, 1, 5, 4, 8, 6, 3 } , { 4, 2, 0, 6, 8, 7, 1, 3, 5, 9 } , _
{ 1, 7, 5, 0, 9, 8, 3, 4, 2, 6 } , { 6, 1, 2, 3, 0, 4, 5, 9, 7, 8 } , _
{ 3, 6, 7, 4, 2, 0, 9, 5, 8, 1 } , { 5, 8, 6, 9, 7, 2, 0, 1, 3, 4 } , _
{ 8, 9, 4, 5, 3, 6, 2, 0, 1, 7 } , { 9, 4, 3, 8, 6, 1, 7, 2, 0, 5 } , _
{ 2, 5, 8, 1, 4, 3, 6, 7, 9, 0 } }
 
Dim As UInteger i, col_i, old_row_i, new_row_i
 
For i = 0 To Len(digit_str) -1
col_i = digit_str[i] - Asc("0")
new_row_i = table(old_row_i, col_i)
old_row_i = new_row_i
Next
 
Return new_row_i
 
End Function
 
' ------=< MAIN >=------
 
Data "5724", "5727", "112946", ""
 
Dim As UInteger checksum, t
Dim As String test_string
 
Do
 
Read test_string
If test_string = "" Then Exit Do
Print "Checksum test: ";test_string;
 
checksum = Damm(test_string)
If checksum = 0 Then
Print " is valid"
Else
Print " is invalid"
End If
 
Loop
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>Checksum test: 5724 is valid
Checksum test: 5727 is invalid
Checksum test: 112946 is valid</pre>
 
==={{header|GW-BASIC}}===
<lang basic>10 DEFINT D,I,X,Y: DIM DT(9,9)
{{works with|BASICA}}
<syntaxhighlight lang="basic">10 DEFINT D,I,X,Y: DIM DT(9,9)
20 FOR Y=0 TO 9: FOR X=0 TO 9: READ DT(X,Y): NEXT X,Y
30 INPUT N$: IF N$="" THEN END
Line 529 ⟶ 729:
170 DATA 8,9,4,5,3,6,2,0,1,7
180 DATA 9,4,3,8,6,1,7,2,0,5
190 DATA 2,5,8,1,4,3,6,7,9,0</langsyntaxhighlight>
{{out}}
<pre>? 5724
PASS
? 5727
FAIL
? 112946
PASS
? 112949
FAIL</pre>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="liberty basic">Dim DT(9, 9)
 
For y = 0 To 9
For x = 0 To 9
Read val
DT(x, y) = val
Next x
Next y
 
Input check$
While (check$ <> "")
D = 0
For i = 1 To Len(check$)
D = DT(Val(Mid$(check$, i, 1)), D)
Next i
If D Then
Print "Invalid"
Else
Print "Valid"
End If
Input check$
Wend
End
 
DATA 0,3,1,7,5,9,8,6,4,2
DATA 7,0,9,2,1,5,4,8,6,3
DATA 4,2,0,6,8,7,1,3,5,9
DATA 1,7,5,0,9,8,3,4,2,6
DATA 6,1,2,3,0,4,5,9,7,8
DATA 3,6,7,4,2,0,9,5,8,1
DATA 5,8,6,9,7,2,0,1,3,4
DATA 8,9,4,5,3,6,2,0,1,7
DATA 9,4,3,8,6,1,7,2,0,5
DATA 2,5,8,1,4,3,6,7,9,0</syntaxhighlight>
{{out}}
<pre>?5724
Valid
?5727
Invalid
?112946
Valid
?112949
Invalid</pre>
 
=== {{header|Nascom BASIC}} ===
<pre>? 5724
{{trans|GW-BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic">10 REM Damm algorithm
20 DIM DT(9,9)
30 FOR Y=0 TO 9:FOR X=0 TO 9
40 READ DT(X,Y)
50 NEXT X:NEXT Y
60 N$="":INPUT N$:IF N$="" THEN 130
70 D=0
80 FOR I=1 TO LEN(N$)
90 D=DT(VAL(MID$(N$,I,1)),D)
100 NEXT I
110 IF D THEN PRINT "FAIL":GOTO 60
120 PRINT "PASS":GOTO 60
130 END
140 DATA 0,3,1,7,5,9,8,6,4,2
150 DATA 7,0,9,2,1,5,4,8,6,3
160 DATA 4,2,0,6,8,7,1,3,5,9
170 DATA 1,7,5,0,9,8,3,4,2,6
180 DATA 6,1,2,3,0,4,5,9,7,8
190 DATA 3,6,7,4,2,0,9,5,8,1
200 DATA 5,8,6,9,7,2,0,1,3,4
210 DATA 8,9,4,5,3,6,2,0,1,7
220 DATA 9,4,3,8,6,1,7,2,0,5
230 DATA 2,5,8,1,4,3,6,7,9,0
</syntaxhighlight>
{{out}}
<pre>
? 5724
PASS
? 5727
Line 541 ⟶ 823:
? 112949
FAIL
?
</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">DataSection
DT_Start:
Data.b 0,3,1,7,5,9,8,6,4,2
Data.b 7,0,9,2,1,5,4,8,6,3
Data.b 4,2,0,6,8,7,1,3,5,9
Data.b 1,7,5,0,9,8,3,4,2,6
Data.b 6,1,2,3,0,4,5,9,7,8
Data.b 3,6,7,4,2,0,9,5,8,1
Data.b 5,8,6,9,7,2,0,1,3,4
Data.b 8,9,4,5,3,6,2,0,1,7
Data.b 9,4,3,8,6,1,7,2,0,5
Data.b 2,5,8,1,4,3,6,7,9,0
EndDataSection
 
Procedure.i Adr(Row,Col) : ProcedureReturn ?DT_Start+Row+10*Col : EndProcedure
 
Procedure.b CheckDamm(Value.s)
*ipc.Character=@Value : it=0
While *ipc\c
it=PeekB(Adr(*ipc\c-'0',it)) : *ipc+SizeOf(Character)
Wend
ProcedureReturn Bool(it)
EndProcedure
 
If OpenConsole()
Repeat
Print("Check Damm: ") : i$=Input()
If CheckDamm(i$) : PrintN(Space(12)+"FALSE") : Else : PrintN(Space(12)+"TRUE") : EndIf
Until i$=""
EndIf
End</syntaxhighlight>
{{out}}
<pre>Check Damm: 5724
TRUE
Check Damm: 5727
FALSE
Check Damm: 112946
TRUE
Check Damm: 112949
FALSE
Check Damm:
</pre>
 
==={{header|uBasic/4tH}}===
{{trans|Visual Basic .NET}}
{{works with|v3.64}}
<syntaxhighlight lang="text">Push 0, 3, 1, 7, 5, 9, 8, 6, 4, 2: i = FUNC(_Data(0))
Push 7, 0, 9, 2, 1, 5, 4, 8, 6, 3: i = FUNC(_Data(i))
Push 4, 2, 0, 6, 8, 7, 1, 3, 5, 9: i = FUNC(_Data(i))
Push 1, 7, 5, 0, 9, 8, 3, 4, 2, 6: i = FUNC(_Data(i))
Push 6, 1, 2, 3, 0, 4, 5, 9, 7, 8: i = FUNC(_Data(i))
Push 3, 6, 7, 4, 2, 0, 9, 5, 8, 1: i = FUNC(_Data(i))
Push 5, 8, 6, 9, 7, 2, 0, 1, 3, 4: i = FUNC(_Data(i))
Push 8, 9, 4, 5, 3, 6, 2, 0, 1, 7: i = FUNC(_Data(i))
Push 9, 4, 3, 8, 6, 1, 7, 2, 0, 5: i = FUNC(_Data(i))
Push 2, 5, 8, 1, 4, 3, 6, 7, 9, 0: i = FUNC(_Data(i))
' Read the table
Push 112949, 112946, 5727, 5724 ' Put numbers on the stack
 
For i = 1 To Used() ' Read up to the number of stack items
Print Using "______"; Tos();" is "; ' Print the header
If FUNC(_Damm (Str(Pop()))) Then Print "in";
Print "valid" ' invalid only if Damm() returns TRUE
Next ' Next stack item
 
End
 
_Data Param (1) ' Reads data in reverse order,
Local (2) ' starting with A@
c@ = a@ + Used() ' Calculate next offset
 
For b@ = c@-1 To a@ Step -1 ' Now place the elements
@(b@) = Pop() ' that are retrieved from the stack
Next b@ ' Next item
 
Return (c@) ' Return new offset
 
 
_Damm Param (1) ' Perform the Damm algorithm
Local (2)
 
c@ = 0 ' Reset the flag
For b@ = 0 To Len(a@) - 1 ' Check all characters in the string
c@ = @(c@*10 + peek(a@, b@) - ord("0"))
Next ' Next character
 
Return (c@) ' Return Flag</syntaxhighlight>
{{Out}}
<pre> 5724 is valid
5727 is invalid
112946 is valid
112949 is invalid
 
0 OK, 0:984</pre>
Although the output of this version is virtually identical, it uses uBasic/4tH features consistently and is consequently much shorter.
<syntaxhighlight lang="text">Proc _IsDamm (5724)
Proc _IsDamm (5727)
Proc _IsDamm (112946)
Proc _IsDamm (112949)
 
End
 
_Damm
Param (1)
Local (1)
 
b@ := "0317598642709215486342068713591750983426612304597836742095815869720134894536201794386172052581436790"
 
Do Until a@ = 0 ' until number is consumed
Push a@ % 10 : a@ = a@ / 10 ' extract digit and put on stack
Loop
 
Do While Used () ' last number retrieved?
a@ = Peek(b@, (a@ * 10) + Pop ()) - Ord ("0")
Loop ' calculate checksum
Return (a@) ' return checksum
 
_IsDamm ' evaluate and print checksum
Param (1)
Print Using "______";a@;" is ";Show (Iif (Func (_Damm (a@)), "invalid", "valid"))
Return</syntaxhighlight>
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
ReadOnly table = {
{0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
{7, 0, 9, 2, 1, 5, 4, 8, 6, 3},
{4, 2, 0, 6, 8, 7, 1, 3, 5, 9},
{1, 7, 5, 0, 9, 8, 3, 4, 2, 6},
{6, 1, 2, 3, 0, 4, 5, 9, 7, 8},
{3, 6, 7, 4, 2, 0, 9, 5, 8, 1},
{5, 8, 6, 9, 7, 2, 0, 1, 3, 4},
{8, 9, 4, 5, 3, 6, 2, 0, 1, 7},
{9, 4, 3, 8, 6, 1, 7, 2, 0, 5},
{2, 5, 8, 1, 4, 3, 6, 7, 9, 0}
}
 
Function Damm(s As String) As Boolean
Dim interim = 0
For Each c In s
interim = table(interim, AscW(c) - AscW("0"))
Next
Return interim = 0
End Function
 
Sub Main()
Dim numbers = {5724, 5727, 112946, 112949}
For Each number In numbers
Dim isvalid = Damm(number.ToString())
If isvalid Then
Console.WriteLine("{0,6} is valid", number)
Else
Console.WriteLine("{0,6} is invalid", number)
End If
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre> 5724 is valid
5727 is invalid
112946 is valid
112949 is invalid</pre>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let dammDamm(ns) = valof
$( let dt = table
0,3,1,7,5,9,8,6,4,2,
Line 574 ⟶ 1,025:
check("112946")
check("112949")
$)</langsyntaxhighlight>
{{out}}
<pre>5724: pass
Line 580 ⟶ 1,031:
112946: pass
112949: fail</pre>
 
=={{header|BQN}}==
 
'''Translation of:''' [[J]]
 
<syntaxhighlight lang="bqn">table ← >⟨ 0‿3‿1‿7‿5‿9‿8‿6‿4‿2
7‿0‿9‿2‿1‿5‿4‿8‿6‿3
4‿2‿0‿6‿8‿7‿1‿3‿5‿9
1‿7‿5‿0‿9‿8‿3‿4‿2‿6
6‿1‿2‿3‿0‿4‿5‿9‿7‿8
3‿6‿7‿4‿2‿0‿9‿5‿8‿1
5‿8‿6‿9‿7‿2‿0‿1‿3‿4
8‿9‿4‿5‿3‿6‿2‿0‿1‿7
9‿4‿3‿8‿6‿1‿7‿2‿0‿5
2‿5‿8‿1‿4‿3‿6‿7‿9‿0 ⟩
 
 
Digits ← 10{⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)}
 
Damm ← {0=0(table⊑˜⋈)˜´⌽Digits 𝕩}
 
Damm¨5724‿5727‿112946</syntaxhighlight>
<syntaxhighlight lang="text">⟨ 1 0 1 ⟩</syntaxhighlight>
 
[https://mlochbaum.github.io/BQN/try.html#code=dGFibGUg4oaQID7in6ggMOKAvzPigL8x4oC/N+KAvzXigL854oC/OOKAvzbigL804oC/MgogICAgICAgICAgIDfigL8w4oC/OeKAvzLigL8x4oC/NeKAvzTigL844oC/NuKAvzMKICAgICAgICAgICA04oC/MuKAvzDigL824oC/OOKAvzfigL8x4oC/M+KAvzXigL85CiAgICAgICAgICAgMeKAvzfigL814oC/MOKAvznigL844oC/M+KAvzTigL8y4oC/NgogICAgICAgICAgIDbigL8x4oC/MuKAvzPigL8w4oC/NOKAvzXigL854oC/N+KAvzgKICAgICAgICAgICAz4oC/NuKAvzfigL804oC/MuKAvzDigL854oC/NeKAvzjigL8xCiAgICAgICAgICAgNeKAvzjigL824oC/OeKAvzfigL8y4oC/MOKAvzHigL8z4oC/NAogICAgICAgICAgIDjigL854oC/NOKAvzXigL8z4oC/NuKAvzLigL8w4oC/MeKAvzcKICAgICAgICAgICA54oC/NOKAvzPigL844oC/NuKAvzHigL834oC/MuKAvzDigL81CiAgICAgICAgICAgMuKAvzXigL844oC/MeKAvzTigL8z4oC/NuKAvzfigL854oC/MCDin6kKCgpEaWdpdHMg4oaQIDEwe+KMvfCdlZd84oyK4oiYw7fin5zwnZWX4o2fKOKGlTErwrfijIrwnZWX4ouG4oG8MeKMiOKKoil9CgpEYW1tIOKGkCB7MD0wKHRhYmxl4oqRy5zii4gpy5zCtOKMvURpZ2l0cyDwnZWpfQoKRGFtbcKoNTcyNOKAvzU3MjfigL8xMTI5NDY= Try It!]
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
Line 611 ⟶ 1,087:
puts(damm(input, 4) ? "Checksum correct" : "Checksum incorrect");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 619 ⟶ 1,095:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace DammAlgorithm {
Line 657 ⟶ 1,133:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 665 ⟶ 1,141:
 
=={{header|C++}}==
===Version 1===
{{trans|C#|C sharp}}
<langsyntaxhighlight lang="cpp">#include <sstreamstring>
 
#include <cstdio>
const int TABLE[][10] = {
 
inline constexper int TABLE[][10] = {
{0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
{7, 0, 9, 2, 1, 5, 4, 8, 6, 3},
Line 681 ⟶ 1,160:
};
 
using[[nodiscard]] bool damm(std::string; s) noexcept {
bool damm(string s) {
int interim = 0;
for (charconst auto c : s) {
interim = TABLE[interim][c - '0'];
}
Line 691 ⟶ 1,169:
 
int main() {
for (const auto numbersnum =: { 5724, 5727, 112946, 112949 };) {
if (damm(std::to_string(num))) {
for (int num : numbers) {
std::printf("%6d is valid\n", num);
using std::stringstream;
}
stringstream ss;
else std::printf("%6d is invalid\n", num);
ss << num;
bool isValid = damm(ss.str());
if (isValid) {
printf("%6d is valid\n", num);
} else {
printf("%6d is invalid\n", num);
}
}
}</syntaxhighlight>
 
return 0;
}</lang>
{{out}}
<pre> 5724 is valid
Line 711 ⟶ 1,181:
112946 is valid
112949 is invalid</pre>
 
===Version 2===
<syntaxhighlight lang="cpp">
// Compile with:
// g++ -std=c++20 -Wall -Wextra -pedantic damm.cpp -o damm
 
#include <iostream>
#include <array> // for std::array
#include <string> // for std::string, std::to_string and std::string::find
 
const std::array<std::array<int, 10>, 10> table = {{ // Operation table
{0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
{7, 0, 9, 2, 1, 5, 4, 8, 6, 3},
{4, 2, 0, 6, 8, 7, 1, 3, 5, 9},
{1, 7, 5, 0, 9, 8, 3, 4, 2, 6},
{6, 1, 2, 3, 0, 4, 5, 9, 7, 8},
{3, 6, 7, 4, 2, 0, 9, 5, 8, 1},
{5, 8, 6, 9, 7, 2, 0, 1, 3, 4},
{8, 9, 4, 5, 3, 6, 2, 0, 1, 7},
{9, 4, 3, 8, 6, 1, 7, 2, 0, 5},
{2, 5, 8, 1, 4, 3, 6, 7, 9, 0}
}};
 
bool damm(int input) {
int interim = 0; // initialise to 0
const std::string digit = "0123456789";
for (const auto c : std::to_string(input))
interim = table[interim][digit.find(c)];
// Process the number digit by digit:
// 1. The column index = number's digit
// 2. The row index = interim digit
// 3. Replace interim digit with table entry (table[<interim digit>][<number's digit>])
return interim == 0; // Is interim digit equals zero? If so, the input is valid, invalid otherwise.
}
 
int main() {
for (const auto num : {5724, 5727, 112946, 112949})
std::cout << num << "\t" << "Checksum is " << (damm(num) ? "valid" : "invalid") << std::endl;
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
5724 Checksum is valid
5727 Checksum is invalid
112946 Checksum is valid
112949 Checksum is invalid
</pre>
 
=={{header|Caché ObjectScript}}==
 
<syntaxhighlight lang="cos">Class Utils.Check [ Abstract ]
{
 
ClassMethod Damm(num As %Integer, mode As %Integer = 1) As %Integer
{
TRY {
I mode=0 RETURN ..Damm(num,2)=0
S res=0, str=num
S table=[
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9],
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6],
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8],
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1],
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0]
]
F i=1:1:$L(str) S res=table.%Get(res).%Get($E(str,i))
I mode=1 S res=num_res
} CATCH {
S res=""
}
Q res
}
 
}</syntaxhighlight>
{{out|Examples}}
<pre>USER>For { Read n Quit:n="" Write ": "_##class(Utils.Check).Damm(n, 0), ! }
5724: 1
5727: 0
112946: 1
112949: 0
 
USER>w ##class(Utils.Check).Damm(11294)
112946
USER></pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(def tbl [[0 3 1 7 5 9 8 6 4 2]
[7 0 9 2 1 5 4 8 6 3]
[4 2 0 6 8 7 1 3 5 9]
Line 726 ⟶ 1,286:
(defn damm? [digits]
(= 0 (reduce #(nth (nth tbl %1) %2) 0
(map #(Character/getNumericValue %) (seq digits)))))</langsyntaxhighlight>
{{Out}}
<pre>=> (damm? "5724")
Line 734 ⟶ 1,294:
=> (damm? "112946")
true</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Verify that the Damm check digit of a string of digits is correct.
% Signals 'bad_format' if the string contains non-digits.
damm = proc (s: string) returns (bool) signals (bad_format)
ai = array[int]
aai = array[ai]
own damm_table: aai := aai$[0:
ai$[0: 0,3,1,7,5,9,8,6,4,2],
ai$[0: 7,0,9,2,1,5,4,8,6,3],
ai$[0: 4,2,0,6,8,7,1,3,5,9],
ai$[0: 1,7,5,0,9,8,3,4,2,6],
ai$[0: 6,1,2,3,0,4,5,9,7,8],
ai$[0: 3,6,7,4,2,0,9,5,8,1],
ai$[0: 5,8,6,9,7,2,0,1,3,4],
ai$[0: 8,9,4,5,3,6,2,0,1,7],
ai$[0: 9,4,3,8,6,1,7,2,0,5],
ai$[0: 2,5,8,1,4,3,6,7,9,0]
]
interim: int := 0
for c: char in string$chars(s) do
d: int := int$parse(string$c2s(c)) resignal bad_format
interim := damm_table[interim][d]
end
return(interim = 0)
end damm
 
% Checks
start_up = proc ()
po: stream := stream$primary_output()
tests: sequence[string] := sequence[string]$[
"5724", "5727", "112946", "112949"
]
for test: string in sequence[string]$elements(tests) do
stream$puts(po, test || ": ")
if damm(test) then
stream$putl(po, "pass")
else
stream$putl(po, "fail")
end
end
end start_up</syntaxhighlight>
{{out}}
<pre>5724: pass
5727: fail
112946: pass
112949: fail</pre>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Damm test on number given as ASCII string
Line 775 ⟶ 1,385:
test("5727");
test("112946");
test("112949");</langsyntaxhighlight>
 
{{out}}
Line 785 ⟶ 1,395:
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.stdio;
 
auto table = [
Line 820 ⟶ 1,430:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 829 ⟶ 1,439:
=={{header|Delphi}}==
See [[#Pascal|Pascal]].
 
=={{header|F#|F sharp}}==
=={{header|Dyalect}}==
<lang fsharp>open System
 
<syntaxhighlight lang="dyalect">let table = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9],
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6],
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8],
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1],
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0]
]
 
func damm(s) {
var interim = 0
for c in s {
interim = table[interim][Integer(c)]
}
return interim == 0;
}
 
let numbers = [5724, 5727, 112946, 112949]
for number in numbers {
let isValid = damm(number.ToString())
if isValid {
print("\(number) is valid")
} else {
print("\(number) is invalid")
}
}</syntaxhighlight>
 
{{out}}
 
<pre>5724 is valid
5727 is invalid
112946 is valid
112949 is invalid</pre>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc damm(*char str) bool:
[10][10]byte dammtbl = (
(0,3,1,7,5,9,8,6,4,2),
(7,0,9,2,1,5,4,8,6,3),
(4,2,0,6,8,7,1,3,5,9),
(1,7,5,0,9,8,3,4,2,6),
(6,1,2,3,0,4,5,9,7,8),
(3,6,7,4,2,0,9,5,8,1),
(5,8,6,9,7,2,0,1,3,4),
(8,9,4,5,3,6,2,0,1,7),
(9,4,3,8,6,1,7,2,0,5),
(2,5,8,1,4,3,6,7,9,0)
);
byte interim;
char c;
channel input text ch;
interim := 0;
open(ch, str);
while read(ch; c) do
interim := dammtbl[interim][c-'0']
od;
close(ch);
interim = 0
corp
 
proc check(*char str) void:
writeln(str, ": ", if damm(str) then "pass" else "fail" fi)
corp
 
proc main() void:
check("5724");
check("5727");
check("112946");
check("112949");
corp</syntaxhighlight>
{{out}}
<pre>5724: pass
5727: fail
112946: pass
112949: fail</pre>
 
=={{header|EasyLang}}==
{{trans|Java}}
<syntaxhighlight>
func damm inp$ .
table[][] = [ [ 0 3 1 7 5 9 8 6 4 2 ] [ 7 0 9 2 1 5 4 8 6 3 ] [ 4 2 0 6 8 7 1 3 5 9 ] [ 1 7 5 0 9 8 3 4 2 6 ] [ 6 1 2 3 0 4 5 9 7 8 ] [ 3 6 7 4 2 0 9 5 8 1 ] [ 5 8 6 9 7 2 0 1 3 4 ] [ 8 9 4 5 3 6 2 0 1 7 ] [ 9 4 3 8 6 1 7 2 0 5 ] [ 2 5 8 1 4 3 6 7 9 0 ] ]
inp[] = number strchars inp$
for v in inp[]
inter = table[inter + 1][v + 1]
.
return if inter = 0
.
nums[] = [ 5724 5727 112946 112949 ]
for v in nums[]
write v & " is "
if damm v = 1
print "valid"
else
print "invalid"
.
.
</syntaxhighlight>
 
=={{header|Excel|Excel}}==
 
Place your number in A1 and the formula at any cell.
 
<syntaxhighlight lang="excel">
=REDUCE(0,MID(A1,SEQUENCE(1,LEN(A1)),1),LAMBDA(i,j,INDEX({0,3,1,7,5,9,8,6,4,2;7,0,9,2,1,5,4,8,6,3;4,2,0,6,8,7,1,3,5,9;1,7,5,0,9,8,3,4,2,6;6,1,2,3,0,4,5,9,7,8;3,6,7,4,2,0,9,5,8,1;5,8,6,9,7,2,0,1,3,4;8,9,4,5,3,6,2,0,1,7;9,4,3,8,6,1,7,2,0,5;2,5,8,1,4,3,6,7,9,0},i+1,j+1)))
</syntaxhighlight>
 
For Google Sheets, you need to use arrayformula
 
<syntaxhighlight lang="excel">
=REDUCE(0,arrayformula(MID(A1,SEQUENCE(1,LEN(A1)),1)),lambda(i,j,INDEX({0,3,1,7,5,9,8,6,4,2;7,0,9,2,1,5,4,8,6,3;4,2,0,6,8,7,1,3,5,9;1,7,5,0,9,8,3,4,2,6;6,1,2,3,0,4,5,9,7,8;3,6,7,4,2,0,9,5,8,1;5,8,6,9,7,2,0,1,3,4;8,9,4,5,3,6,2,0,1,7;9,4,3,8,6,1,7,2,0,5;2,5,8,1,4,3,6,7,9,0},i+1,j+1)))
</syntaxhighlight>
 
For countries that uses comma as a decimal divisor, use:
 
<syntaxhighlight lang="excel">
=REDUCE(0;MID(A2;SEQUENCE(1;LEN(A2));1);lambda(i;j;INDEX({0\3\1\7\5\9\8\6\4\2;7\0\9\2\1\5\4\8\6\3;4\2\0\6\8\7\1\3\5\9;1\7\5\0\9\8\3\4\2\6;6\1\2\3\0\4\5\9\7\8;3\6\7\4\2\0\9\5\8\1;5\8\6\9\7\2\0\1\3\4;8\9\4\5\3\6\2\0\1\7;9\4\3\8\6\1\7\2\0\5;2\5\8\1\4\3\6\7\9\0};i+1;j+1)))
</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang="fsharp">open System
 
let TABLE = [|
Line 862 ⟶ 1,598:
 
0 // return an integer exit code
</syntaxhighlight>
</lang>
{{out}}
<pre> 5724 is valid
Line 870 ⟶ 1,606:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: interpolate kernel math math.parser qw sequences ;
 
CONSTANT: table
Line 890 ⟶ 1,626:
 
qw{ 5724 5727 112946 112949 }
[ dup damm? "" "in" ? [I ${} is ${}validI] nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 904 ⟶ 1,640:
{{works with|gforth|0.7.3}}
 
<langsyntaxhighlight lang="forth">: newdigit ( col row -- u ) 10 * + C" 0317598642709215486342068713591750983426612304597836742095815869720134894536201794386172052581436790" 1+ + c@ 48 - ;
: nextdigit ( addr -- addr+1 u ) dup c@ 48 - swap 1+ swap ;
 
Line 920 ⟶ 1,656:
2dup damm
rot rot type 48 + emit
;</langsyntaxhighlight>
 
{{out}}
Line 931 ⟶ 1,667:
Thanks to the ability standardised in F90 to define an array with a lower bound other than one, this can be achieved without the need for annoying offsets, as in A(i + 1) instead of just A(i). However, right from the start, Fortran has stored arrays in column-major order, so statements that initialise two-dimensional arrays via a list of consecutive values can look odd when they are nicely laid out, because they will have to be be in transposed order. Alternatively, if the layout is the same as the expected (row,column) usage, the actual usage of the array will have to be (column,row). Rather than transpose a ten by ten matrix, this is the approach here. The continuation column has the (apparent) row count, but row zero can't have the digit zero in the continuation column as this is taken to be equivalent to a space (meaning "no continuation") just in case it is used for the first line of a statement to be continued. However, the letter o will do instead. A capital O looks too much like a 0...
Possibly a more useful function would be one that returned the check digit that must be appended to a sequence to provide the full sequence, as when preparing a checksummed sequence for output. For checking input, such a function would be applied to all but the last digit of the suspect sequence, and its result compared to the supplied last digit. But for simplicity here, all that is reported is "good" or "bad", without hints as to what would have been good. <langsyntaxhighlight Fortranlang="fortran"> LOGICAL FUNCTION DAMM(DIGIT) !Check that a sequence of digits checks out..
Calculates according to the method of H. Michael Damm, described in 2004.
CHARACTER*(*) DIGIT !A sequence of digits only.
Line 962 ⟶ 1,698:
WRITE (6,*) DAMM("112946"),"112946"
END</langsyntaxhighlight>
Output:
<pre> T 5724
Line 968 ⟶ 1,704:
T 112946</pre>
 
=={{header|FreeBASICFōrmulæ}}==
<lang freebasic>' version 04-07-2018
' compile with: fbc -s console
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Damm_algorithm}}
Function Damm(digit_str As String) As UInteger
 
'''Solution'''
Dim As UInteger table(10,10) => { { 0, 3, 1, 7, 5, 9, 8, 6, 4, 2 } , _
{ 7, 0, 9, 2, 1, 5, 4, 8, 6, 3 } , { 4, 2, 0, 6, 8, 7, 1, 3, 5, 9 } , _
{ 1, 7, 5, 0, 9, 8, 3, 4, 2, 6 } , { 6, 1, 2, 3, 0, 4, 5, 9, 7, 8 } , _
{ 3, 6, 7, 4, 2, 0, 9, 5, 8, 1 } , { 5, 8, 6, 9, 7, 2, 0, 1, 3, 4 } , _
{ 8, 9, 4, 5, 3, 6, 2, 0, 1, 7 } , { 9, 4, 3, 8, 6, 1, 7, 2, 0, 5 } , _
{ 2, 5, 8, 1, 4, 3, 6, 7, 9, 0 } }
 
[[File:Fōrmulæ - Damm algorithm 01.png]]
Dim As UInteger i, col_i, old_row_i, new_row_i
 
For i = 0 To Len(digit_str) -1
col_i = digit_str[i] - Asc("0")
new_row_i = table(old_row_i, col_i)
old_row_i = new_row_i
Next
 
Return new_row_i
 
End Function
 
' ------=< MAIN >=------
 
Data "5724", "5727", "112946", ""
 
Dim As UInteger checksum, t
Dim As String test_string
 
Do
 
Read test_string
If test_string = "" Then Exit Do
Print "Checksum test: ";test_string;
 
checksum = Damm(test_string)
If checksum = 0 Then
Print " is valid"
Else
Print " is invalid"
End If
 
Loop
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>Checksum test: 5724 is valid
Checksum test: 5727 is invalid
Checksum test: 112946 is valid</pre>
 
=={{header|Fōrmulæ}}==
 
'''Test cases'''
In [https://wiki.formulae.org/Damm_algorithm this] page you can see the solution of this task.
 
[[File:Fōrmulæ - Damm algorithm 02.png]]
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
[[File:Fōrmulæ - Damm algorithm 03.png]]
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,063 ⟶ 1,748:
fmt.Printf("%6s %t\n", s, damm(s))
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 true
Line 1,072 ⟶ 1,757:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class DammAlgorithm {
private static final int[][] TABLE = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 1,103 ⟶ 1,788:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 1,110 ⟶ 1,795:
112949 is invalid</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Char (digitToInt)
import Text.Printf (printf)
 
Line 1,130 ⟶ 1,815:
main :: IO ()
main = mapM_ (uncurry(printf "%6s is valid: %s\n") . ((,) <*> show . damm) . show)
[5724, 5727, 112946, 112949]</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid: True
Line 1,139 ⟶ 1,824:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">OpTbl=: _99 ". ];._2 noun define
0 3 1 7 5 9 8 6 4 2
7 0 9 2 1 5 4 8 6 3
Line 1,161 ⟶ 1,846:
)
 
checkDamm=: 0 = getDamm</langsyntaxhighlight>
'''Example Usage:'''
<langsyntaxhighlight lang="j"> checkDamm&> 5724 5727 112946
1 0 1</langsyntaxhighlight>
 
Or,
 
<syntaxhighlight lang="j">checkdamm=: {{0=((".;._2{{)n
0 7 4 1 6 3 5 8 9 2
3 0 2 7 1 6 8 9 4 5
1 9 0 5 2 7 6 4 3 8
7 2 6 0 3 4 9 5 8 1
5 1 8 9 0 2 7 3 6 4
9 5 7 8 4 0 2 6 1 3
8 4 1 3 5 9 0 2 7 6
6 8 3 4 9 5 1 0 2 7
4 6 5 2 7 8 3 1 0 9
2 3 9 6 8 1 4 7 5 0
}}){~<@,)/|.0,10#.inv y}}"0</syntaxhighlight>
 
<syntaxhighlight lang="j"> checkdamm 5724 5727 112946
1 0 1</syntaxhighlight>
 
We could probably replace that embedded table with something more concise if the description of the math behind the algorithm on the wikipedia page was more complete. (See talk page.)
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class DammAlgorithm {
private static final int[][] table = {
{0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
Line 1,199 ⟶ 1,904:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 1,207 ⟶ 1,912:
 
=={{header|JavaScript}}==
<langsyntaxhighlight JavaScriptlang="javascript">const table = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
Line 1,228 ⟶ 1,933:
);
test();
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,238 ⟶ 1,943:
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def checkdigit:
[[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
Line 1,258 ⟶ 1,963:
| checkdigit as $d
| [., $d]
</syntaxhighlight>
</lang>
{{out}}
<syntaxhighlight lang="sh">
<lang sh>
[5724,true]
[5727,false]
[112946,true]
[112949,false]
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function checkdigit(n)
matrix = (
(0, 3, 1, 7, 5, 9, 8, 6, 4, 2),
Line 1,288 ⟶ 1,993:
 
foreach(i -> println("$i validates as: ", checkdigit(string(i)) == 0), [5724, 5727, 112946])
</langsyntaxhighlight>{{output}}
<pre>
5724 validates as: true
Line 1,296 ⟶ 2,001:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
val table = arrayOf(
Line 1,323 ⟶ 2,028:
println("${"%6d".format(number)} is ${if (isValid) "valid" else "invalid"}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,332 ⟶ 2,037:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local tab = {
{0,3,1,7,5,9,8,6,4,2}, {7,0,9,2,1,5,4,8,6,3},
{4,2,0,6,8,7,1,3,5,9}, {1,7,5,0,9,8,3,4,2,6},
Line 1,355 ⟶ 2,060:
if not r then io.write( "in" ) end
io.write( "valid!\n" )
end</langsyntaxhighlight>
{{out}}
<pre>Enter the number to check: 5724
Line 1,365 ⟶ 2,070:
Enter the number to check: 0</pre>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Damm_Algorithm{
Function Prepare {
Line 1,407 ⟶ 2,112:
}
Damm_Algorithm
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,415 ⟶ 2,120:
112940 5 False
</pre>
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE DAMMAL
.MCALL .GTLIN,.PRINT,.EXIT
DAMMAL::JMP DEMO
 
; VALIDATE DAMM STRING IN R0; ZERO FLAG SET IF VALID
DAMM: CLR R2 ; INTERIM DIGIT
BR 2$
1$: SUB #60,R1 ; DIGIT?
BCS 3$ ; IF NOT, NOT VALID
CMP R1,#^D9
BGT 3$
MOV R2,R3 ; CALCULATE DAMM TABLE INDEX
ASL R3
ASL R3
ADD R2,R3
ASL R3
ADD R1,R3
MOVB 4$(R3),R2 ; GET NEW INTERIM DIGIT FROM TABLE
2$: MOVB (R0)+,R1 ; NEXT CHAR
BNE 1$ ; END OF STRING?
TST R2 ; IF SO, CHECK IF INTERIM DIGIT IS 0
3$: RTS PC
4$: .BYTE ^D0,^D3,^D1,^D7,^D5,^D9,^D8,^D6,^D4,^D2
.BYTE ^D7,^D0,^D9,^D2,^D1,^D5,^D4,^D8,^D6,^D3
.BYTE ^D4,^D2,^D0,^D6,^D8,^D7,^D1,^D3,^D5,^D9
.BYTE ^D1,^D7,^D5,^D0,^D9,^D8,^D3,^D4,^D2,^D6
.BYTE ^D6,^D1,^D2,^D3,^D0,^D4,^D5,^D9,^D7,^D8
.BYTE ^D3,^D6,^D7,^D4,^D2,^D0,^D9,^D5,^D8,^D1
.BYTE ^D5,^D8,^D6,^D9,^D7,^D2,^D0,^D1,^D3,^D4
.BYTE ^D8,^D9,^D4,^D5,^D3,^D6,^D2,^D0,^D1,^D7
.BYTE ^D9,^D4,^D4,^D8,^D6,^D1,^D7,^D2,^D0,^D5
.BYTE ^D2,^D5,^D8,^D1,^D4,^D3,^D6,^D7,^D9,^D0
 
DEMO: .GTLIN #5$ ; READ LINE
MOV #5$,R0
TSTB (R0) ; EMPTY LINE?
BNE 1$
.EXIT ; IF SO, STOP
1$: JSR PC,DAMM ; TEST LINE
BNE 2$ ; FAIL?
.PRINT #3$
BR DEMO
2$: .PRINT #4$ ; PASS?
BR DEMO
3$: .ASCIZ /PASS/
4$: .ASCIZ /FAIL/
5$: .BLKB 200
.END DAMMAL</syntaxhighlight>
{{out}}
<pre>5724
PASS
5727
FAIL
112946
PASS
112949
FAIL</pre>
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
 
R VERIFY DAMM CHECKSUM OF NUMBER
Line 1,454 ⟶ 2,217:
VECTOR VALUES INVAL = $I9,S1,7HINVALID*$
END OF PROGRAM
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,462 ⟶ 2,225:
112946 VALID
112949 INVALID</pre>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">matrix = {{0, 3, 1, 7, 5, 9, 8, 6, 4, 2}, {7, 0, 9, 2, 1, 5, 4, 8, 6,
3}, {4, 2, 0, 6, 8, 7, 1, 3, 5, 9}, {1, 7, 5, 0, 9, 8, 3, 4, 2,
6}, {6, 1, 2, 3, 0, 4, 5, 9, 7, 8}, {3, 6, 7, 4, 2, 0, 9, 5, 8,
Line 1,480 ⟶ 2,242:
row == 0
]
Damm /@ {5724, 5727, 112946}</langsyntaxhighlight>
{{out}}
<pre>{True, False, True}</pre>
 
=={{header|MATLAB}}==
{{trans|Julia}}
<syntaxhighlight lang="MATLAB}}">
clear all;close all;clc;
 
% Test the function with the provided numbers
numbers = [5724, 5727, 112946];
for i = 1:length(numbers)
if checkdigit(numbers(i))
fprintf('%d validates as: true\n', numbers(i));
else
fprintf('%d validates as: false\n', numbers(i));
end
end
 
function isValid = checkdigit(n)
matrix = [
0, 3, 1, 7, 5, 9, 8, 6, 4, 2;
7, 0, 9, 2, 1, 5, 4, 8, 6, 3;
4, 2, 0, 6, 8, 7, 1, 3, 5, 9;
1, 7, 5, 0, 9, 8, 3, 4, 2, 6;
6, 1, 2, 3, 0, 4, 5, 9, 7, 8;
3, 6, 7, 4, 2, 0, 9, 5, 8, 1;
5, 8, 6, 9, 7, 2, 0, 1, 3, 4;
8, 9, 4, 5, 3, 6, 2, 0, 1, 7;
9, 4, 3, 8, 6, 1, 7, 2, 0, 5;
2, 5, 8, 1, 4, 3, 6, 7, 9, 0
];
 
row = 0;
nString = num2str(n);
for i = 1:length(nString)
d = str2double(nString(i));
row = matrix(row+1, d + 1);
end
isValid = (row == 0);
end
</syntaxhighlight>
{{out}}
<pre>
5724 validates as: true
5727 validates as: false
112946 validates as: true
</pre>
 
=={{header|Modula-2}}==
{{Output?}}
<langsyntaxhighlight lang="modula2">MODULE DammAlgorithm;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,540 ⟶ 2,347:
 
ReadChar;
END DammAlgorithm.</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<syntaxhighlight lang="modula3">MODULE DammAlgorithm EXPORTS Main;
 
IMPORT IO, Text;
 
VAR
Numbers:ARRAY[0..3] OF TEXT := ARRAY OF TEXT{"5724", "5727", "112946", "112949"};
PROCEDURE Damm(READONLY Str:TEXT):BOOLEAN =
TYPE
TTable = ARRAY[0..9],[0..9] OF INTEGER;
VAR
Table := TTable
{ARRAY OF INTEGER{0,3,1,7,5,9,8,6,4,2},
ARRAY OF INTEGER{7,0,9,2,1,5,4,8,6,3},
ARRAY OF INTEGER{4,2,0,6,8,7,1,3,5,9},
ARRAY OF INTEGER{1,7,5,0,9,8,3,4,2,6},
ARRAY OF INTEGER{6,1,2,3,0,4,5,9,7,8},
ARRAY OF INTEGER{3,6,7,4,2,0,9,5,8,1},
ARRAY OF INTEGER{5,8,6,9,7,2,0,1,3,4},
ARRAY OF INTEGER{8,9,4,5,3,6,2,0,1,7},
ARRAY OF INTEGER{9,4,3,8,6,1,7,2,0,5},
ARRAY OF INTEGER{2,5,8,1,4,3,6,7,9,0}};
Interim,I:INTEGER := 0;
BEGIN
WHILE I <= Text.Length(Str)-1 DO
Interim := Table[Interim, ORD(Text.GetChar(Str, I)) - ORD('0')];
INC(I);
END;
RETURN Interim = 0;
END Damm;
 
BEGIN
FOR I := FIRST(Numbers) TO LAST(Numbers) DO
IF Damm(Numbers[I]) THEN
IO.Put(Numbers[I] & " is valid\n");
ELSE
IO.Put(Numbers[I] & " is invalid\n");
END;
END;
END DammAlgorithm.</syntaxhighlight>
{{out}}
<pre>5724 is valid
5727 is invalid
112946 is valid
112949 is invalid</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
<lang Nim>
from algorithm import reverse
 
Line 1,586 ⟶ 2,441:
checkData([Digit 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 6, 7, 8, 9, 0, 1])
checkData([Digit 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 6, 7, 8, 9, 0, 8])
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,597 ⟶ 2,452:
=={{header|Objeck}}==
{{trans|C#}}
<langsyntaxhighlight lang="objeck">class DammAlgorithm {
@table : static : Int[,];
 
Line 1,633 ⟶ 2,488:
return interim = 0;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,645 ⟶ 2,500:
=={{header|Pascal}}==
{{works with|Free Pascal}} {{trans|Modula-2}} nearly copy&paste
<langsyntaxhighlight lang="pascal">program DammAlgorithm;
uses
sysutils;
Line 1,697 ⟶ 2,552:
Print(112949);
Readln;
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,707 ⟶ 2,562:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub damm {
my(@digits) = split '', @_[0];
my @tbl =([< 0 3 1 7 5 9 8 6 4 2 >],
Line 1,727 ⟶ 2,582:
for (5724, 5727, 112946) {
print "$_:\tChecksum digit @{[damm($_) ? '' : 'in']}correct.\n"
}</langsyntaxhighlight>
{{out}}
<pre>5724: Checksum digit correct.
Line 1,736 ⟶ 2,591:
As phix uses 1-based indexes, 1 must be added to the operation table,
and validity is given by ending on one, rather than zero.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">tbl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">},</span>
Line 1,763 ⟶ 2,618:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%7s is %svalid\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">damm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"in"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,773 ⟶ 2,628:
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php"><?php
function lookup($r,$c) {
$table = array(
Line 1,797 ⟶ 2,652:
echo "{$i} is ".(isDammValid($i) ? "valid" : "invalid")."<br>";
}
?></langsyntaxhighlight>
{{out}}
<pre>
Line 1,807 ⟶ 2,662:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(setq *D
(quote
(0 3 1 7 5 9 8 6 4 2)
Line 1,827 ⟶ 2,682:
(println (damm? 5727))
(println (damm? 112946))
(println (damm? 112940))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,837 ⟶ 2,692:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
 
/* DAMM CHECKSUM FOR DECIMAL NUMBER IN GIVEN STRING */
Line 1,892 ⟶ 2,747:
 
CALL BDOS(0,0);
EOF</langsyntaxhighlight>
{{out}}
<pre>5724: PASS
Line 1,899 ⟶ 2,754:
112949: FAIL</pre>
 
=={{header|PureBasicPowerShell}}==
{{trans|C#}}
<lang PureBasic>DataSection
<syntaxhighlight lang="powershell">
DT_Start:
$table = (
Data.b 0,3,1,7,5,9,8,6,4,2
Data.b 7, (0,9,2 3, 1, 7, 5,4 9, 8, 6,3 4, 2),
Data.b 4 (7, 0, 9, 2,0 1,6 5, 4, 8,7,1 6, 3),5,9
Data.b 1 (4,7,5 2, 0,9 6, 8, 7, 1, 3,4 5,2 9),6
Data.b 6, (1,2 7,3 5, 0,4 9,5 8,9 3,7 4, 2, 6),8
Data.b 3, (6,7,4 1, 2, 3, 0,9 4, 5, 9, 7, 8),1
Data.b 5,8 (3, 6,9, 7, 4, 2, 0,1 9,3 5, 8, 1),4
Data.b 8,9,4, (5,3 8, 6, 9, 7, 2, 0, 1,7 3, 4),
Data.b (8, 9, 4, 5, 3,8, 6,1,7, 2, 0,5 1, 7),
Data.b 2,5,8,1 (9, 4, 3, 8, 6, 1, 7,9 2, 0, 5),
(2, 5, 8, 1, 4, 3, 6, 7, 9, 0)
EndDataSection
)
 
function Test-Damm([string]$s) {
Procedure.i Adr(Row,Col) : ProcedureReturn ?DT_Start+Row+10*Col : EndProcedure
$interim = 0
foreach ($c in $s.ToCharArray()) {
$interim = $table[$interim][[int]$c - [int][char]'0']
}
return $interim -eq 0
}
 
foreach ($number in 5724, 5727, 112946, 112949) {
Procedure.b CheckDamm(Value.s)
$validity = if (Test-Damm $number) {'valid'} else {'invalid'}
*ipc.Character=@Value : it=0
'{0,6} is {1}' -f $number, $validity
While *ipc\c
}
it=PeekB(Adr(*ipc\c-'0',it)) : *ipc+SizeOf(Character)
</syntaxhighlight>
Wend
ProcedureReturn Bool(it)
EndProcedure
 
If OpenConsole()
Repeat
Print("Check Damm: ") : i$=Input()
If CheckDamm(i$) : PrintN(Space(12)+"FALSE") : Else : PrintN(Space(12)+"TRUE") : EndIf
Until i$=""
EndIf
End</lang>
{{out}}
<pre>Check Damm: 5724
5724 is valid
TRUE
Check Damm: 5727 is invalid
112946 is valid
FALSE
112949 is invalid
Check Damm: 112946
TRUE
Check Damm: 112949
FALSE
Check Damm:
</pre>
 
=={{header|Python}}==
<langsyntaxhighlight Pythonlang="python">def damm(num: int) -> bool:
row = 0
for digit in str(num):
Line 1,965 ⟶ 2,813:
if __name__ == '__main__':
for test in [5724, 5727, 112946]:
print(f'{test}\t Validates as: {damm(test)}')</langsyntaxhighlight>
{{out}}
<pre>5724 Validates as: True
Line 1,973 ⟶ 2,821:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 swap witheach
[ char 0 - dip
[ table
Line 1,995 ⟶ 2,843:
 
$ "5724 5725 112946 112949"
nest$ witheach validate</langsyntaxhighlight>
 
{{out}}
Line 2,003 ⟶ 2,851:
112946 is valid.
112949 is not valid.</pre>
 
=={{header|R}}==
<syntaxhighlight lang="r">Damm_algo <- function(number){
row_i = 0
iterable = strsplit(toString(number), "")[[1]]
validation_matrix =
matrix(
c(
0, 3, 1, 7, 5, 9, 8, 6, 4, 2,
7, 0, 9, 2, 1, 5, 4, 8, 6, 3,
4, 2, 0, 6, 8, 7, 1, 3, 5, 9,
1, 7, 5, 0, 9, 8, 3, 4, 2, 6,
6, 1, 2, 3, 0, 4, 5, 9, 7, 8,
3, 6, 7, 4, 2, 0, 9, 5, 8, 1,
5, 8, 6, 9, 7, 2, 0, 1, 3, 4,
8, 9, 4, 5, 3, 6, 2, 0, 1, 7,
9, 4, 3, 8, 6, 1, 7, 2, 0, 5,
2, 5, 8, 1, 4, 3, 6, 7, 9, 0),
nrow = 10, ncol = 10, byrow = T
)
for(digit in as.integer(iterable)){
row_i = validation_matrix[row_i + 1, digit + 1] #in R indexes start from 1 and not from zero
}
test <- ifelse(row_i == 0, "VALID", "NOT VALID")
message(paste("Number", number, "is", test))
}
 
for(number in c(5724, 5727, 112946, 112949)){
Damm_algo(number)
}</syntaxhighlight>
 
{{out}}
<pre>
Number 5724 is VALID
Number 5727 is NOT VALID
Number 112946 is VALID
Number 112949 is NOT VALID
</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/match)
 
Line 2,046 ⟶ 2,936:
(check-true (valid-number? 5724))
(check-false (valid-number? 5274))
(check-true (valid-number? 112946)))</langsyntaxhighlight>
No output from checks means that all tests passed.
 
 
=={{header|Raku}}==
Line 2,053 ⟶ 2,944:
{{works with|Rakudo|2017.05}}
 
<syntaxhighlight lang="raku" perl6line>sub damm ( *@digits ) {
my @tbl = [0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
Line 2,072 ⟶ 2,963:
for 5724, 5727, 112946 {
say "$_:\tChecksum digit { damm( $_.comb ) ?? '' !! 'in' }correct."
}</langsyntaxhighlight>
{{out}}
<pre>5724: Checksum digit correct.
5727: Checksum digit incorrect.
112946: Checksum digit correct.</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Test '5724'>
<Test '5727'>
<Test '112946'>
<Test '112949'>;
};
 
Test {
e.Ds = <Prout e.Ds ': ' <Damm e.Ds>>;
};
 
Damm {
('0') = Pass;
(s.Int) = Fail;
(s.Int) s.D e.Ds,
<Item <Numb s.Int> <DammTable>>: (e.Row),
<Item <Numb s.D> e.Row>: s.Next
= <Damm (s.Next) e.Ds>;
e.Ds = <Damm ('0') e.Ds>;
};
 
DammTable {
= ('0317598642')
('7092154863')
('4206871359')
('1750983426')
('6123045978')
('3674209581')
('5869720134')
('8945362017')
('9438617205')
('2581436790')
};
 
Item {
0 t.I e.X = t.I;
s.N t.I e.X = <Item <- s.N 1> e.X>;
};</syntaxhighlight>
{{out}}
<pre>5724: Pass
5727: Fail
112946: Pass
112949: Fail</pre>
 
=={{header|REXX}}==
===manufactured table===
<langsyntaxhighlight lang="rexx">/* REXX */
Call init
Call test 5724
Line 2,122 ⟶ 3,058:
grid.i.col=word(list,col+2)
End
Return</langsyntaxhighlight>
{{out}}
<pre>5724 is ok
Line 2,131 ⟶ 3,067:
=== static table ===
Using a static table is over four times faster than manufacturing it.
<langsyntaxhighlight lang="rexx">/*REXX pgm uses H. Michael Damm's algorithm to validate numbers with suffixed check sum. digit*/
@ a.0= 0317598642; @a.1= 7092154863; @a.2= 4206871359; @a.3= 1750983426; @a.4= 6123045978
@ a.5= 3674209581; @a.6= 5869720134; @a.7= 8945362017; @a.8= 9438617205; @a.9= 2581436790
callCall Damm 5724, 5727, 112946, 112940 112940 /*invoke Damm's algorithm forTo some #'s.*/
exit 0 Exit /*stick a forkTok in it, we're all doneDone. */
/*---------------------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Damm:
Damm: do j=1 for arg(); x= arg(j); $= 0; z= right(x, 1)
Do j=1 To arg() do p=1 for length(x); g=$; $= substr(@.$, 1 + substr(x, p,/* 1),loop over numbers 1) */
x=arg(j)
end /*p*/
d=0
if $==0 then say ' valid checksum digit ' z " for " x
Do p=1 To length(x)-1 else say ' invalid checksum digit ' z "/* forcompute "the checksum xdigit ' (should be' g")"*/
d=substr(a.d,substr(x,p,1)+1,1)
end /*j*/; return</lang>
end /*p*/
z=right(x,1) /* the given digit */
If z=d Then Say ' valid checksum digit ' z " for " x
Else Say ' invalid checksum digit ' z " for " x ' (should be' d")"
End /*j*/
Return /syntaxhighlight>
{{out|output|text=&nbsp; when using the (internal) default inputs:}}
<pre>
Line 2,152 ⟶ 3,094:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring"># Project : Damm algorithm
 
matrix = [[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 2,178 ⟶ 3,120:
else
return " is invalid"
ok</langsyntaxhighlight>
Output:
<pre>5724 is valid
Line 2,184 ⟶ 3,126:
112946 is valid</pre>
 
=={{header|RubyRPL}}==
{{trans|Python}}
<lang ruby>def damm_valid? nbr
'''Array version'''
idx = 0
≪ →STR
for i in 0 .. nbr.length - 1
[[ 0 3 1 a7 =5 9 8 6 4 2 nbr[i].to_i
[ 7 0 return9 false2 if1 a5 ==4 nil8 6 3 ]
[ 4 2 idx0 =6 8 7 1 3 5 9 @table[idx][a]
[ 1 7 5 0 9 8 3 4 2 6 ]
end
idx ==[ 6 1 2 3 0 4 5 9 7 8 ]
[ 3 6 7 4 2 0 9 5 8 1 ]
end
[ 5 8 6 9 7 2 0 1 3 4 ]
[ 8 9 4 5 3 6 2 0 1 7 ]
[ 9 4 3 8 6 1 7 2 0 5 ]
[ 2 5 8 1 4 3 6 7 9 0 ]]
→ num table
≪ 0
1 num SIZE '''FOR''' d
1 + num d DUP SUB STR→ 1 +
2 →LIST table SWAP GET
'''NEXT'''
NOT
≫ ≫ '<span style="color:blue">DAMM?</span>' STO
'''String version'''
 
Program flow is quite the same, but storing the table needs only 210 nibbles of RAM, instead of 1625 with a 2-dimensional array.
@table = Array.new [
≪ →STR "0317598642709215486342068713591750983426612304597836742095815869720134894536201794386172052581436790"
→ num table
≪ 0
1 num SIZE '''FOR''' d
10 * num d DUP SUB STR→ 1 + +
table SWAP DUP SUB STR→
'''NEXT'''
NOT
≫ ≫ '<span style="color:blue">DAMM?</span>' STO
 
≪ { 5724 5727 112946 } { }
1 3 PICK SIZE '''FOR''' j
OVER j GET '<span style="color:blue">DAMM?</span>' STO "True" "False" IFTE '''NEXT'''
≫ EVAL
{{out}}
<pre>
2: { 5724 5727 112946 }
1: { "True" "False" True" }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">TABLE = [
[0,3,1,7,5,9,8,6,4,2], [7,0,9,2,1,5,4,8,6,3],
[4,2,0,6,8,7,1,3,5,9], [1,7,5,0,9,8,3,4,2,6],
Line 2,203 ⟶ 3,180:
]
 
def damm_valid?(n) = n.digits.reverse.inject(0){|idx, a| TABLE[idx][a] } == 0
while true
 
print "Number to check: "
[5724, 5727, 112946].each{|n| puts if"#{n}: #{damm_valid?(n) gets.chomp? "" : "in"}valid"}
</syntaxhighlight>
puts "this number is valid!"
{{out}}<pre>5724: valid
else
puts "this number is5727: invalid!"
112946: valid
end
end</langpre>
{{out}}<pre>Number to check: 5724
this number is valid!
Number to check: 5727
this number is invalid!
Number to check: 112940
this number is invalid!
Number to check: 112946
this number is valid!
Number to check: 1321
this number is valid!</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn damm(number: &str) -> u8 {
static TABLE: [[u8; 10]; 10] = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 2,257 ⟶ 3,224:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 2,265 ⟶ 3,232:
=={{header|Scala}}==
===Functional, (tail) recursive, concise and clean===
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
 
object DammAlgorithm extends App {
Line 2,293 ⟶ 3,260:
for (number <- numbers) println(f"$number%6d is ${damm(number.toString, 0)}.")
 
}</langsyntaxhighlight>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/d25pzoH/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/8t9RuipwRHGGoczFXPvT5A Scastie (remote JVM)].
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program damm_algorithm;
tests := [5724, 5727, 112946, 112949];
 
loop for test in tests do
print(str test + ': ' + if damm test then 'Pass' else 'Fail' end);
end loop;
 
op damm(n);
dt := [[0,3,1,7,5,9,8,6,4,2],
[7,0,9,2,1,5,4,8,6,3],
[4,2,0,6,8,7,1,3,5,9],
[1,7,5,0,9,8,3,4,2,6],
[6,1,2,3,0,4,5,9,7,8],
[3,6,7,3,2,0,9,5,8,1],
[5,8,6,9,7,2,0,1,3,4],
[8,9,4,5,3,6,2,0,1,7],
[9,4,3,8,6,1,7,2,0,5],
[2,5,8,1,4,3,6,7,9,0]];
 
i := 0;
loop for d in str n do
i := dt(i+1)(val d+1);
end loop;
return i=0;
end op;
end program;</syntaxhighlight>
{{out}}
<pre>5724: Pass
5727: Fail
112946: Pass
112949: Fail</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func damm(digits) {
static tbl = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 2,316 ⟶ 3,316:
for n in [5724, 5727, 112946] {
say "#{n}:\tChecksum digit #{ damm(n.digits) ? '' : 'in'}correct."
}</langsyntaxhighlight>
{{out}}
<pre>5724: Checksum digit correct.
Line 2,322 ⟶ 3,322:
112946: Checksum digit correct.</pre>
 
=={{header|uBasic/4tHTcl}}==
{{trans|Visual Basic .NETPerl}}
<syntaxhighlight lang="Tcl">proc damm {number} {
{{works with|v3.64}}
set digits [split $number ""]
<lang>Push 0, 3, 1, 7, 5, 9, 8, 6, 4, 2: i = FUNC(_Data(0))
set tbl {
Push 7, 0, 9, 2, 1, 5, 4, 8, 6, 3: i = FUNC(_Data(i))
Push 4, 2, 0, 6, 8, 7, 1, {0 3, 1 7 5, 9: i8 =6 FUNC(_Data(i))4 2}
Push 1, 7, 5, {7 0, 9, 8,2 3,1 5 4, 2,8 6: i = FUNC(_Data(i))3}
Push 6, 1, 2, 3, 0, {4, 5,2 9,0 7,6 8: i7 =1 FUNC(_Data(i))3 5 9}
Push 3, 6, 7, 4, 2, 0, 9, {1 7 5, 0 9 8, 1:3 i4 =2 FUNC(_Data(i))6}
Push 5, 8, 6, 9, 7, 2, 0, {6 1, 2 3, 0 4: i5 =9 FUNC(_Data(i))7 8}
Push 8, 9, 4, 5, {3, 6, 7 4 2, 0, 1,9 7:5 i8 = FUNC(_Data(i))1}
Push 9, 4, 3, {5 8, 6, 1,9 7, 2, 0, 5:1 i3 = FUNC(_Data(i))4}
Push 2, 5, {8, 1,9 4, 5 3, 6, 7, 9,2 0: i1 = FUNC(_Data(i))7}
{9 4 3 8 6 1 7 2 0 ' Read the table5}
Push 112949, 112946, 5727, 5724 {2 5 8 1 '4 Put3 numbers6 on7 the9 stack0}
}
 
set row 0
For i = 1 To Used() ' Read up to the number of stack items
foreach col $digits {
Print Using "______"; Tos();" is "; ' Print the header
set row [lindex $tbl $row $col]
If FUNC(_Damm (Str(Pop()))) Then Print "in";
}
Print "valid" ' invalid only if Damm() returns TRUE
return [expr {$row == 0 ? 1 : 0}]
Next ' Next stack item
}
 
foreach number {5724 5727 112946} {
End
set correct [damm $number]
puts "$number:\tChecksum digit [expr {$correct ? "" : "in"}]correct."
}</syntaxhighlight>
 
{{out}}
_Data Param (1) ' Reads data in reverse order,
<pre>
Local (2) ' starting with A@
5724: Checksum digit correct.
5727: Checksum digit incorrect.
c@ = a@ + Used() ' Calculate next offset
112946: Checksum digit correct.
</pre>
 
=={{header|V (Vlang)}}==
For b@ = c@-1 To a@ Step -1 ' Now place the elements
{{trans|Go}}
@(b@) = Pop() ' that are retrieved from the stack
<syntaxhighlight lang="v (vlang)">const table = [
Next b@ ' Next item
[u8(0), 3, 1, 7, 5, 9, 8, 6, 4, 2],
[u8(7), 0, 9, 2, 1, 5, 4, 8, 6, 3],
[u8(4), 2, 0, 6, 8, 7, 1, 3, 5, 9],
[u8(1), 7, 5, 0, 9, 8, 3, 4, 2, 6],
[u8(6), 1, 2, 3, 0, 4, 5, 9, 7, 8],
[u8(3), 6, 7, 4, 2, 0, 9, 5, 8, 1],
[u8(5), 8, 6, 9, 7, 2, 0, 1, 3, 4],
[u8(8), 9, 4, 5, 3, 6, 2, 0, 1, 7],
[u8(9), 4, 3, 8, 6, 1, 7, 2, 0, 5],
[u8(2), 5, 8, 1, 4, 3, 6, 7, 9, 0],
]
fn damm(input string) bool {
mut interim := u8(0)
for c in input.bytes() {
interim = table[interim][c-'0'[0]]
}
return interim == 0
}
fn main() {
for s in ["5724", "5727", "112946", "112949"] {
println("${s:6} ${damm(s)}")
}
}</syntaxhighlight>
 
Return (c@) ' Return new offset
 
 
_Damm Param (1) ' Perform the Damm algorithm
Local (2)
 
c@ = 0 ' Reset the flag
For b@ = 0 To Len(a@) - 1 ' Check all characters in the string
c@ = @(c@*10 + peek(a@, b@) - ord("0"))
Next ' Next character
 
Return (c@) ' Return Flag</lang>
{{Out}}
<pre> 5724 is valid
5727 is invalid
112946 is valid
112949 is invalid
 
0 OK, 0:984</pre>
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<lang vbnet>Module Module1
 
ReadOnly table = {
{0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
{7, 0, 9, 2, 1, 5, 4, 8, 6, 3},
{4, 2, 0, 6, 8, 7, 1, 3, 5, 9},
{1, 7, 5, 0, 9, 8, 3, 4, 2, 6},
{6, 1, 2, 3, 0, 4, 5, 9, 7, 8},
{3, 6, 7, 4, 2, 0, 9, 5, 8, 1},
{5, 8, 6, 9, 7, 2, 0, 1, 3, 4},
{8, 9, 4, 5, 3, 6, 2, 0, 1, 7},
{9, 4, 3, 8, 6, 1, 7, 2, 0, 5},
{2, 5, 8, 1, 4, 3, 6, 7, 9, 0}
}
 
Function Damm(s As String) As Boolean
Dim interim = 0
For Each c In s
interim = table(interim, AscW(c) - AscW("0"))
Next
Return interim = 0
End Function
 
Sub Main()
Dim numbers = {5724, 5727, 112946, 112949}
For Each number In numbers
Dim isvalid = Damm(number.ToString())
If isvalid Then
Console.WriteLine("{0,6} is valid", number)
Else
Console.WriteLine("{0,6} is invalid", number)
End If
Next
End Sub
 
End Module</lang>
{{out}}
<pre> 5724 is valid
57275724 is invalidtrue
5727 false
112946 is valid
112946 true
112949 is invalid</pre>
112949 false
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var table = [
Line 2,443 ⟶ 3,420:
 
for (s in ["5724", "5727", "112946", "112949"]) {
SystemFmt.print("%(Fmt.$6s $s(6", s)), %(damm.call(s))")
}</langsyntaxhighlight>
 
{{out}}
Line 2,450 ⟶ 3,427:
5724 true
5727 false
112946 true
112949 false
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">string 0; \use zero-terminated strings
 
func Damm(Input); \Return 'true' if checksum is correct
char Input;
int Interim, Table;
[Table:= [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9],
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6],
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8],
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1],
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0] ];
Interim:= 0;
while Input(0) do
[Interim:= Table(Interim, Input(0)-^0);
Input:= Input+1;
];
return Interim = 0;
];
 
int String, I;
[String:= ["5724", "5727", "112946", "112949"];
for I:= 0 to 4-1 do
[Text(0, String(I)); ChOut(0, 9\tab\);
Text(0, if Damm(String(I)) then "true" else "false");
CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
5724 true
5727 false
112946 true
112949 false
Line 2,455 ⟶ 3,473:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn damm(digits){ // digits is something that supports an iterator of integers
var [const] tbl=Data(0,Int, // 10x10 byte bucket
0, 3, 1, 7, 5, 9, 8, 6, 4, 2,
Line 2,468 ⟶ 3,486:
2, 5, 8, 1, 4, 3, 6, 7, 9, 0);
0 == digits.reduce(fcn(interim,digit){ tbl[interim*10 + digit] },0)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">damm(List(5,7,2,4)).println(); // True
damm(Data(0,Int,5,7,2,7).howza(0)).println(); // stream bytes, False
damm((112946).split()).println(); // True</langsyntaxhighlight>
{{out}}
<pre>True
2,094

edits