Damm algorithm: Difference between revisions

m
syntax highlighting fixup automation
(Added Modula-3)
m (syntax highlighting fixup automation)
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 46:
=={{header|8080 Assembly}}==
 
<langsyntaxhighlight lang="8080asm"> org 100h
jmp demo
;;; Given an 0-terminated ASCII string containing digits in [DE],
Line 99:
jmp 5
no: db 'NOT '
ok: db 'OK$'</langsyntaxhighlight>
 
{{out}}
Line 114:
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm"> cpu 8086
bits 16
section .text
Line 159:
section .data
no: db 'NOT '
ok: db 'OK$'</langsyntaxhighlight>
 
{{out}}
Line 174:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">BYTE FUNC Damm(CHAR ARRAY a)
BYTE ARRAY table=[
0 3 1 7 5 9 8 6 4 2
Line 219:
Test("112946")
Test("112949")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Damm_algorithm.png Screenshot from Atari 8-bit computer]
Line 230:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Damm_Algorithm is
Line 268:
Put_Damm ("112946");
Put_Damm ("112949");
end Damm_Algorithm;</langsyntaxhighlight>
 
{{out}}
Line 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 328:
test damm algorithm( "5727", FALSE );
test damm algorithm( "112946", TRUE )
END</langsyntaxhighlight>
{{out}}
<pre>check digit of 5724 is valid
Line 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 351:
tbl⍪← 2 5 8 1 4 3 6 7 9 0
0={tbl[⍵;⍺]}/⌽0,⍵
}</langsyntaxhighlight>
 
{{out}}
Line 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 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 461:
swi #0
pass: .ascii "Pass\n"
fail: .ascii "Fail\n"</langsyntaxhighlight>
 
{{out}}
Line 473:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">; by @Krenium
 
table: [
Line 509:
]
 
loop ["5724" "5727" "112946" "112949"] => test</langsyntaxhighlight>
 
{{out}}
Line 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 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 545:
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk"># syntax: GAWK -f DAMM_ALGORITHM.AWK
BEGIN {
damm_init()
Line 574:
damm[8] = "9438617205"
damm[9] = "2581436790"
}</langsyntaxhighlight>
{{out}}
<pre>T 5724
Line 582:
=={{header|BASIC}}==
 
<langsyntaxhighlight 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 598:
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}}
Line 613:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let damm(ns) = valof
Line 643:
check("112946")
check("112949")
$)</langsyntaxhighlight>
{{out}}
<pre>5724: pass
Line 654:
'''Translation of:''' [[J]]
 
<langsyntaxhighlight 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
Line 670:
Damm ← {0=0(table⊑˜⋈)˜´⌽Digits 𝕩}
 
Damm¨5724‿5727‿112946</langsyntaxhighlight>
<syntaxhighlight lang="text">⟨ 1 0 1 ⟩</langsyntaxhighlight>
 
[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 705:
puts(damm(input, 4) ? "Checksum correct" : "Checksum incorrect");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 713:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace DammAlgorithm {
Line 751:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 760:
=={{header|C++}}==
{{trans|C#|C sharp}}
<langsyntaxhighlight lang="cpp">#include <sstream>
 
const int TABLE[][10] = {
Line 799:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 808:
=={{header|Caché ObjectScript}}==
 
<langsyntaxhighlight lang="cos">Class Utils.Check [ Abstract ]
{
 
Line 836:
}
 
}</langsyntaxhighlight>
{{out|Examples}}
<pre>USER>For { Read n Quit:n="" Write ": "_##class(Utils.Check).Damm(n, 0), ! }
Line 849:
 
=={{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 862:
(defn damm? [digits]
(= 0 (reduce #(nth (nth tbl %1) %2) 0
(map #(Character/getNumericValue %) (seq digits)))))</langsyntaxhighlight>
{{Out}}
<pre>=> (damm? "5724")
Line 872:
 
=={{header|CLU}}==
<langsyntaxhighlight 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)
Line 914:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>5724: pass
Line 922:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Damm test on number given as ASCII string
Line 961:
test("5727");
test("112946");
test("112949");</langsyntaxhighlight>
 
{{out}}
Line 971:
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.stdio;
 
auto table = [
Line 1,006:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 1,018:
=={{header|Dyalect}}==
 
<langsyntaxhighlight lang="dyalect">let table = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
Line 1,047:
print("\(number) is invalid")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,057:
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">proc damm(*char str) bool:
[10][10]byte dammtbl = (
(0,3,1,7,5,9,8,6,4,2),
Line 1,092:
check("112946");
check("112949");
corp</langsyntaxhighlight>
{{out}}
<pre>5724: pass
Line 1,100:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
let TABLE = [|
Line 1,132:
 
0 // return an integer exit code
</syntaxhighlight>
</lang>
{{out}}
<pre> 5724 is valid
Line 1,140:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: interpolate kernel math math.parser qw sequences ;
 
CONSTANT: table
Line 1,160:
 
qw{ 5724 5727 112946 112949 }
[ dup damm? "" "in" ? [I ${} is ${}validI] nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,174:
{{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 1,190:
2dup damm
rot rot type 48 + emit
;</langsyntaxhighlight>
 
{{out}}
Line 1,201:
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 1,232:
WRITE (6,*) DAMM("112946"),"112946"
END</langsyntaxhighlight>
Output:
<pre> T 5724
Line 1,239:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 04-07-2018
' compile with: fbc -s console
 
Line 1,289:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Checksum test: 5724 is valid
Line 1,304:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,333:
fmt.Printf("%6s %t\n", s, damm(s))
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 true
Line 1,342:
=={{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,373:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 1,380:
112949 is invalid</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Char (digitToInt)
import Text.Printf (printf)
 
Line 1,400:
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,409:
=={{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,431:
)
 
checkDamm=: 0 = getDamm</langsyntaxhighlight>
'''Example Usage:'''
<langsyntaxhighlight lang="j"> checkDamm&> 5724 5727 112946
1 0 1</langsyntaxhighlight>
 
Or,
 
<langsyntaxhighlight Jlang="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
Line 1,449:
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</langsyntaxhighlight>
 
<langsyntaxhighlight Jlang="j"> checkdamm 5724 5727 112946
1 0 1</langsyntaxhighlight>
 
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.)
Line 1,458:
=={{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,489:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 1,497:
 
=={{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,518:
);
test();
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,528:
 
=={{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,548:
| 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,578:
 
foreach(i -> println("$i validates as: ", checkdigit(string(i)) == 0), [5724, 5727, 112946])
</langsyntaxhighlight>{{output}}
<pre>
5724 validates as: true
Line 1,586:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
val table = arrayOf(
Line 1,613:
println("${"%6d".format(number)} is ${if (isValid) "valid" else "invalid"}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,622:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="liberty basic">Dim DT(9, 9)
 
For y = 0 To 9
Line 1,655:
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</langsyntaxhighlight>
{{out}}
<pre>?5724
Line 1,667:
 
=={{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,690:
if not r then io.write( "in" ) end
io.write( "valid!\n" )
end</langsyntaxhighlight>
{{out}}
<pre>Enter the number to check: 5724
Line 1,700:
Enter the number to check: 0</pre>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Damm_Algorithm{
Function Prepare {
Line 1,742:
}
Damm_Algorithm
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,751:
</pre>
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
 
R VERIFY DAMM CHECKSUM OF NUMBER
Line 1,789:
VECTOR VALUES INVAL = $I9,S1,7HINVALID*$
END OF PROGRAM
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,800:
 
=={{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,815:
row == 0
]
Damm /@ {5724, 5727, 112946}</langsyntaxhighlight>
{{out}}
<pre>{True, False, True}</pre>
Line 1,821:
=={{header|Modula-2}}==
{{Output?}}
<langsyntaxhighlight lang="modula2">MODULE DammAlgorithm;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,875:
 
ReadChar;
END DammAlgorithm.</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE DammAlgorithm EXPORTS Main;
 
IMPORT IO, Text;
Line 1,917:
END;
END;
END DammAlgorithm.</langsyntaxhighlight>
{{out}}
<pre>5724 is valid
Line 1,925:
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
<lang Nim>
from algorithm import reverse
 
Line 1,969:
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,980:
=={{header|Objeck}}==
{{trans|C#}}
<langsyntaxhighlight lang="objeck">class DammAlgorithm {
@table : static : Int[,];
 
Line 2,016:
return interim = 0;
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,028:
=={{header|Pascal}}==
{{works with|Free Pascal}} {{trans|Modula-2}} nearly copy&paste
<langsyntaxhighlight lang="pascal">program DammAlgorithm;
uses
sysutils;
Line 2,080:
Print(112949);
Readln;
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,090:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub damm {
my(@digits) = split '', @_[0];
my @tbl =([< 0 3 1 7 5 9 8 6 4 2 >],
Line 2,110:
for (5724, 5727, 112946) {
print "$_:\tChecksum digit @{[damm($_) ? '' : 'in']}correct.\n"
}</langsyntaxhighlight>
{{out}}
<pre>5724: Checksum digit correct.
Line 2,119:
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 2,146:
<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 2,156:
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php"><?php
function lookup($r,$c) {
$table = array(
Line 2,180:
echo "{$i} is ".(isDammValid($i) ? "valid" : "invalid")."<br>";
}
?></langsyntaxhighlight>
{{out}}
<pre>
Line 2,190:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(setq *D
(quote
(0 3 1 7 5 9 8 6 4 2)
Line 2,210:
(println (damm? 5727))
(println (damm? 112946))
(println (damm? 112940))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,220:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
 
/* DAMM CHECKSUM FOR DECIMAL NUMBER IN GIVEN STRING */
Line 2,275:
 
CALL BDOS(0,0);
EOF</langsyntaxhighlight>
{{out}}
<pre>5724: PASS
Line 2,284:
=={{header|PowerShell}}==
{{trans|C#}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
$table = (
(0, 3, 1, 7, 5, 9, 8, 6, 4, 2),
Line 2,310:
'{0,6} is {1}' -f $number, $validity
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,320:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">DataSection
DT_Start:
Data.b 0,3,1,7,5,9,8,6,4,2
Line 2,350:
Until i$=""
EndIf
End</langsyntaxhighlight>
{{out}}
<pre>Check Damm: 5724
Line 2,364:
 
=={{header|Python}}==
<langsyntaxhighlight Pythonlang="python">def damm(num: int) -> bool:
row = 0
for digit in str(num):
Line 2,385:
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 2,393:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 swap witheach
[ char 0 - dip
[ table
Line 2,415:
 
$ "5724 5725 112946 112949"
nest$ witheach validate</langsyntaxhighlight>
 
{{out}}
Line 2,425:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">Damm_algo <- function(number){
row_i = 0
Line 2,456:
for(number in c(5724, 5727, 112946, 112949)){
Damm_algo(number)
}</langsyntaxhighlight>
 
{{out}}
Line 2,467:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/match)
 
Line 2,508:
(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.
 
Line 2,516:
{{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,535:
for 5724, 5727, 112946 {
say "$_:\tChecksum digit { damm( $_.comb ) ?? '' !! 'in' }correct."
}</langsyntaxhighlight>
{{out}}
<pre>5724: Checksum digit correct.
Line 2,543:
=={{header|REXX}}==
===manufactured table===
<langsyntaxhighlight lang="rexx">/* REXX */
Call init
Call test 5724
Line 2,585:
grid.i.col=word(list,col+2)
End
Return</langsyntaxhighlight>
{{out}}
<pre>5724 is ok
Line 2,594:
=== 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.*/
@.0= 0317598642; @.1= 7092154863; @.2= 4206871359; @.3= 1750983426; @.4= 6123045978
@.5= 3674209581; @.6= 5869720134; @.7= 8945362017; @.8= 9438617205; @.9= 2581436790
Line 2,605:
if $==0 then say ' valid checksum digit ' z " for " x
else say ' invalid checksum digit ' z " for " x ' (should be' g")"
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the (internal) default inputs:}}
<pre>
Line 2,615:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring"># Project : Damm algorithm
 
matrix = [[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 2,641:
else
return " is invalid"
ok</langsyntaxhighlight>
Output:
<pre>5724 is valid
Line 2,648:
 
=={{header|Ruby}}==
<langsyntaxhighlight 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,659:
 
[5724, 5727, 112946].each{|n| puts "#{n}: #{damm_valid?(n) ? "" : "in"}valid"}
</syntaxhighlight>
</lang>
{{out}}<pre>5724: valid
5727: invalid
Line 2,666:
 
=={{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,700:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 2,708:
=={{header|Scala}}==
===Functional, (tail) recursive, concise and clean===
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
 
object DammAlgorithm extends App {
Line 2,736:
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|Sidef}}==
<langsyntaxhighlight lang="ruby">func damm(digits) {
static tbl = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 2,759:
for n in [5724, 5727, 112946] {
say "#{n}:\tChecksum digit #{ damm(n.digits) ? '' : 'in'}correct."
}</langsyntaxhighlight>
{{out}}
<pre>5724: Checksum digit correct.
Line 2,768:
{{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))
Line 2,809:
Next ' Next character
 
Return (c@) ' Return Flag</langsyntaxhighlight>
{{Out}}
<pre> 5724 is valid
Line 2,818:
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)
Line 2,843:
Param (1)
Print Using "______";a@;" is ";Show (Iif (Func (_Damm (a@)), "invalid", "valid"))
Return</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
ReadOnly table = {
Line 2,882:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 2,891:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">const table = [
[u8(0), 3, 1, 7, 5, 9, 8, 6, 4, 2],
[u8(7), 0, 9, 2, 1, 5, 4, 8, 6, 3],
Line 2,916:
println("${s:6} ${damm(s)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,929:
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var table = [
Line 2,952:
for (s in ["5724", "5727", "112946", "112949"]) {
System.print("%(Fmt.s(6, s)) %(damm.call(s))")
}</langsyntaxhighlight>
 
{{out}}
Line 2,963:
 
=={{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,976:
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
10,327

edits