Damm algorithm: Difference between revisions
m
syntax highlighting fixup automation
Deadmarshal (talk | contribs) (Added Modula-3) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 15:
=={{header|11l}}==
{{trans|Python}}
<
[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))</
{{out}}
<pre>
Line 46:
=={{header|8080 Assembly}}==
<
jmp demo
;;; Given an 0-terminated ASCII string containing digits in [DE],
Line 99:
jmp 5
no: db 'NOT '
ok: db 'OK$'</
{{out}}
Line 114:
=={{header|8086 Assembly}}==
<
bits 16
section .text
Line 159:
section .data
no: db 'NOT '
ok: db 'OK$'</
{{out}}
Line 174:
=={{header|Action!}}==
<
BYTE ARRAY table=[
0 3 1 7 5 9 8 6 4 2
Line 219:
Test("112946")
Test("112949")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Damm_algorithm.png Screenshot from Atari 8-bit computer]
Line 230:
=={{header|Ada}}==
<
procedure Damm_Algorithm is
Line 268:
Put_Damm ("112946");
Put_Damm ("112949");
end Damm_Algorithm;</
{{out}}
Line 278:
=={{header|ALGOL 68}}==
<
# 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</
{{out}}
<pre>check digit of 5724 is valid
Line 339:
This is a function that takes a vector of digits and returns a boolean.
<
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,⍵
}</
{{out}}
Line 366:
=={{header|AppleScript}}==
<
-- 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</
{{output}}
<
=={{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"</
{{out}}
Line 473:
=={{header|Arturo}}==
<
table: [
Line 509:
]
loop ["5724" "5727" "112946" "112949"] => test</
{{out}}
Line 519:
=={{header|AutoHotkey}}==
<
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])
}</
Examples:<
for i, num in [5724, 5727, 112946, 112949]
result .= num "`tis " (Damm(num) ? "valid" : "not valid") "`n"
MsgBox % result</
Outputs:<pre>5724 is valid
5727 is not valid
Line 545:
=={{header|AWK}}==
<
BEGIN {
damm_init()
Line 574:
damm[8] = "9438617205"
damm[9] = "2581436790"
}</
{{out}}
<pre>T 5724
Line 582:
=={{header|BASIC}}==
<
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</
{{out}}
Line 613:
=={{header|BCPL}}==
<
let damm(ns) = valof
Line 643:
check("112946")
check("112949")
$)</
{{out}}
<pre>5724: pass
Line 654:
'''Translation of:''' [[J]]
<
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</
<syntaxhighlight lang="text">⟨ 1 0 1 ⟩</
[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}}==
<
#include <stddef.h>
#include <stdio.h>
Line 705:
puts(damm(input, 4) ? "Checksum correct" : "Checksum incorrect");
return 0;
}</
{{out}}
Line 713:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace DammAlgorithm {
Line 751:
}
}
}</
{{out}}
<pre> 5724 is valid
Line 760:
=={{header|C++}}==
{{trans|C#|C sharp}}
<
const int TABLE[][10] = {
Line 799:
return 0;
}</
{{out}}
<pre> 5724 is valid
Line 808:
=={{header|Caché ObjectScript}}==
<
{
Line 836:
}
}</
{{out|Examples}}
<pre>USER>For { Read n Quit:n="" Write ": "_##class(Utils.Check).Damm(n, 0), ! }
Line 849:
=={{header|Clojure}}==
<
[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)))))</
{{Out}}
<pre>=> (damm? "5724")
Line 872:
=={{header|CLU}}==
<
% 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</
{{out}}
<pre>5724: pass
Line 922:
=={{header|Cowgol}}==
<
# Damm test on number given as ASCII string
Line 961:
test("5727");
test("112946");
test("112949");</
{{out}}
Line 971:
=={{header|D}}==
<
auto table = [
Line 1,006:
}
}
}</
{{out}}
<pre> 5724 is valid
Line 1,018:
=={{header|Dyalect}}==
<
[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")
}
}</
{{out}}
Line 1,057:
=={{header|Draco}}==
<
[10][10]byte dammtbl = (
(0,3,1,7,5,9,8,6,4,2),
Line 1,092:
check("112946");
check("112949");
corp</
{{out}}
<pre>5724: pass
Line 1,100:
=={{header|F Sharp|F#}}==
<
let TABLE = [|
Line 1,132:
0 // return an integer exit code
</syntaxhighlight>
{{out}}
<pre> 5724 is valid
Line 1,140:
=={{header|Factor}}==
<
CONSTANT: table
Line 1,160:
qw{ 5724 5727 112946 112949 }
[ dup damm? "" "in" ? [I ${} is ${}validI] nl ] each</
{{out}}
<pre>
Line 1,174:
{{works with|gforth|0.7.3}}
<
: nextdigit ( addr -- addr+1 u ) dup c@ 48 - swap 1+ swap ;
Line 1,190:
2dup damm
rot rot type 48 + emit
;</
{{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. <
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</
Output:
<pre> T 5724
Line 1,239:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,289:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>Checksum test: 5724 is valid
Line 1,304:
=={{header|Go}}==
<
import "fmt"
Line 1,333:
fmt.Printf("%6s %t\n", s, damm(s))
}
}</
{{out}}
<pre> 5724 true
Line 1,342:
=={{header|Groovy}}==
{{trans|Java}}
<
private static final int[][] TABLE = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 1,373:
}
}
}</
{{out}}
<pre> 5724 is valid
Line 1,380:
112949 is invalid</pre>
=={{header|Haskell}}==
<
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]</
{{out}}
<pre> 5724 is valid: True
Line 1,409:
=={{header|J}}==
'''Solution:'''
<
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</
'''Example Usage:'''
<
1 0 1</
Or,
<
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</
<
1 0 1</
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}}
<
private static final int[][] table = {
{0, 3, 1, 7, 5, 9, 8, 6, 4, 2},
Line 1,489:
}
}
}</
{{out}}
<pre> 5724 is valid
Line 1,497:
=={{header|JavaScript}}==
<
[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>
{{out}}
<pre>
Line 1,528:
=={{header|jq}}==
<
[[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>
{{out}}
<syntaxhighlight lang="sh">
[5724,true]
[5727,false]
[112946,true]
[112949,false]
</syntaxhighlight>
=={{header|Julia}}==
<
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])
</
<pre>
5724 validates as: true
Line 1,586:
=={{header|Kotlin}}==
<
val table = arrayOf(
Line 1,613:
println("${"%6d".format(number)} is ${if (isValid) "valid" else "invalid"}")
}
}</
{{out}}
Line 1,622:
=={{header|Liberty BASIC}}==
<
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</
{{out}}
<pre>?5724
Line 1,667:
=={{header|Lua}}==
<
{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</
{{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">
Module Damm_Algorithm{
Function Prepare {
Line 1,742:
}
Damm_Algorithm
</syntaxhighlight>
{{out}}
<pre>
Line 1,751:
</pre>
=={{header|MAD}}==
<
R VERIFY DAMM CHECKSUM OF NUMBER
Line 1,789:
VECTOR VALUES INVAL = $I9,S1,7HINVALID*$
END OF PROGRAM
</syntaxhighlight>
{{out}}
Line 1,800:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
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}</
{{out}}
<pre>{True, False, True}</pre>
Line 1,821:
=={{header|Modula-2}}==
{{Output?}}
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,875:
ReadChar;
END DammAlgorithm.</
=={{header|Modula-3}}==
<
IMPORT IO, Text;
Line 1,917:
END;
END;
END DammAlgorithm.</
{{out}}
<pre>5724 is valid
Line 1,925:
=={{header|Nim}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 1,980:
=={{header|Objeck}}==
{{trans|C#}}
<
@table : static : Int[,];
Line 2,016:
return interim = 0;
}
}</
{{out}}
Line 2,028:
=={{header|Pascal}}==
{{works with|Free Pascal}} {{trans|Modula-2}} nearly copy&paste
<
uses
sysutils;
Line 2,080:
Print(112949);
Readln;
END.</
{{out}}
<pre>
Line 2,090:
=={{header|Perl}}==
<
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"
}</
{{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.
<!--<
<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>
<!--</
{{out}}
<pre>
Line 2,156:
=={{header|PHP}}==
<
function lookup($r,$c) {
$table = array(
Line 2,180:
echo "{$i} is ".(isDammValid($i) ? "valid" : "invalid")."<br>";
}
?></
{{out}}
<pre>
Line 2,190:
=={{header|PicoLisp}}==
<
(quote
(0 3 1 7 5 9 8 6 4 2)
Line 2,210:
(println (damm? 5727))
(println (damm? 112946))
(println (damm? 112940))</
{{out}}
<pre>
Line 2,220:
=={{header|PL/M}}==
<
/* DAMM CHECKSUM FOR DECIMAL NUMBER IN GIVEN STRING */
Line 2,275:
CALL BDOS(0,0);
EOF</
{{out}}
<pre>5724: PASS
Line 2,284:
=={{header|PowerShell}}==
{{trans|C#}}
<syntaxhighlight lang="powershell">
$table = (
(0, 3, 1, 7, 5, 9, 8, 6, 4, 2),
Line 2,310:
'{0,6} is {1}' -f $number, $validity
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,320:
=={{header|PureBasic}}==
<
DT_Start:
Data.b 0,3,1,7,5,9,8,6,4,2
Line 2,350:
Until i$=""
EndIf
End</
{{out}}
<pre>Check Damm: 5724
Line 2,364:
=={{header|Python}}==
<
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)}')</
{{out}}
<pre>5724 Validates as: True
Line 2,393:
=={{header|Quackery}}==
<
[ char 0 - dip
[ table
Line 2,415:
$ "5724 5725 112946 112949"
nest$ witheach validate</
{{out}}
Line 2,425:
=={{header|R}}==
<
row_i = 0
Line 2,456:
for(number in c(5724, 5727, 112946, 112949)){
Damm_algo(number)
}</
{{out}}
Line 2,467:
=={{header|Racket}}==
<
(require racket/match)
Line 2,508:
(check-true (valid-number? 5724))
(check-false (valid-number? 5274))
(check-true (valid-number? 112946)))</
No output from checks means that all tests passed.
Line 2,516:
{{works with|Rakudo|2017.05}}
<syntaxhighlight lang="raku"
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."
}</
{{out}}
<pre>5724: Checksum digit correct.
Line 2,543:
=={{header|REXX}}==
===manufactured table===
<
Call init
Call test 5724
Line 2,585:
grid.i.col=word(list,col+2)
End
Return</
{{out}}
<pre>5724 is ok
Line 2,594:
=== static table ===
Using a static table is over four times faster than manufacturing it.
<
@.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</
{{out|output|text= when using the (internal) default inputs:}}
<pre>
Line 2,615:
=={{header|Ring}}==
<
matrix = [[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 2,641:
else
return " is invalid"
ok</
Output:
<pre>5724 is valid
Line 2,648:
=={{header|Ruby}}==
<
[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>
{{out}}<pre>5724: valid
5727: invalid
Line 2,666:
=={{header|Rust}}==
<
static TABLE: [[u8; 10]; 10] = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
Line 2,700:
}
}
}</
{{out}}
<pre> 5724 is valid
Line 2,708:
=={{header|Scala}}==
===Functional, (tail) recursive, concise and clean===
<
object DammAlgorithm extends App {
Line 2,736:
for (number <- numbers) println(f"$number%6d is ${damm(number.toString, 0)}.")
}</
{{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}}==
<
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."
}</
{{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</
{{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</
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
ReadOnly table = {
Line 2,882:
End Sub
End Module</
{{out}}
<pre> 5724 is valid
Line 2,891:
=={{header|Vlang}}==
{{trans|Go}}
<
[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)}")
}
}</
{{out}}
Line 2,929:
{{trans|Go}}
{{libheader|Wren-fmt}}
<
var table = [
Line 2,952:
for (s in ["5724", "5727", "112946", "112949"]) {
System.print("%(Fmt.s(6, s)) %(damm.call(s))")
}</
{{out}}
Line 2,963:
=={{header|zkl}}==
<
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)
}</
<
damm(Data(0,Int,5,7,2,7).howza(0)).println(); // stream bytes, False
damm((112946).split()).println(); // True</
{{out}}
<pre>True
|