Damm algorithm: Difference between revisions
Add SETL
m (→{{header|BASIC}}: GW-BASIC headered.) |
Not a robot (talk | contribs) (Add SETL) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 581:
=={{header|BASIC}}==
==={{header|
{{
{{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
Line 608 ⟶ 624:
PASS
? 112949
FAIL
?
</pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">arraybase 1
global matrix
Line 635 ⟶ 653:
{{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}}===
{{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
40 D=0
50 FOR I=1 TO LEN(N$): D=DT(VAL(MID$(N$,I,1)),D): NEXT I
60 IF D THEN PRINT "FAIL" ELSE PRINT "PASS"
70 GOTO 30
100 DATA 0,3,1,7,5,9,8,6,4,2
110 DATA 7,0,9,2,1,5,4,8,6,3
120 DATA 4,2,0,6,8,7,1,3,5,9
130 DATA 1,7,5,0,9,8,3,4,2,6
140 DATA 6,1,2,3,0,4,5,9,7,8
150 DATA 3,6,7,4,2,0,9,5,8,1
160 DATA 5,8,6,9,7,2,0,1,3,4
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</syntaxhighlight>
{{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}} ===
{{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
FAIL
? 112946
PASS
? 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}}==
Line 1,345 ⟶ 1,703:
F 5727
T 112946</pre>
=={{header|Fōrmulæ}}==
Line 1,734 ⟶ 2,035:
112946 is valid
112949 is invalid</pre>
=={{header|Lua}}==
Line 2,533 ⟶ 2,789:
112946 is valid
112949 is invalid
</pre>
Line 2,756 ⟶ 2,968:
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}}==
Line 3,005 ⟶ 3,262:
}</syntaxhighlight>
{{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}}==
Line 3,067 ⟶ 3,357:
112946: Checksum digit correct.
</pre>
=={{header|V (Vlang)}}==
|