Verhoeff algorithm: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 23: | Line 23: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V MULTIPLICATION_TABLE = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
||
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5], |
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5], |
||
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6], |
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6], |
||
Line 77: | Line 77: | ||
(Int64(1234567890120), 1B, 0B, 0B), |
(Int64(1234567890120), 1B, 0B, 0B), |
||
(Int64(1234567890129), 1B, 0B, 0B)] |
(Int64(1234567890129), 1B, 0B, 0B)] |
||
verhoeffchecksum(n, va, t, ve)</ |
verhoeffchecksum(n, va, t, ve)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 83: | Line 83: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <assert.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 147: | Line 147: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 230: | Line 230: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Verhoeff algorithm. Nigel Galloway: August 26th., 2021 |
// Verhoeff algorithm. Nigel Galloway: August 26th., 2021 |
||
let d,inv,p=let d=[|0;1;2;3;4;5;6;7;8;9;1;2;3;4;0;6;7;8;9;5;2;3;4;0;1;7;8;9;5;6;3;4;0;1;2;8;9;5;6;7;4;0;1;2;3;9;5;6;7;8;5;9;8;7;6;0;4;3;2;1;6;5;9;8;7;1;0;4;3;2;7;6;5;9;8;2;1;0;4;3;8;7;6;5;9;3;2;1;0;4;9;8;7;6;5;4;3;2;1;0|] |
let d,inv,p=let d=[|0;1;2;3;4;5;6;7;8;9;1;2;3;4;0;6;7;8;9;5;2;3;4;0;1;7;8;9;5;6;3;4;0;1;2;8;9;5;6;7;4;0;1;2;3;9;5;6;7;8;5;9;8;7;6;0;4;3;2;1;6;5;9;8;7;1;0;4;3;2;7;6;5;9;8;2;1;0;4;3;8;7;6;5;9;3;2;1;0;4;9;8;7;6;5;4;3;2;1;0|] |
||
Line 253: | Line 253: | ||
printfn $"The assertion that 1234567890120 is valid is %A{csum 1234567890120I=0}" |
printfn $"The assertion that 1234567890120 is valid is %A{csum 1234567890120I=0}" |
||
printfn $"The assertion that 1234567890129 is valid is %A{csum 1234567890129I=0}" |
printfn $"The assertion that 1234567890129 is valid is %A{csum 1234567890129I=0}" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 327: | Line 327: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 404: | Line 404: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 415: | Line 415: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">cyc=: | +/~@i. NB. cyclic group, order y |
||
ac=: |(+-/~@i.) NB. anticyclic group, order y |
ac=: |(+-/~@i.) NB. anticyclic group, order y |
||
a2n=: (+#)@ NB. add 2^n |
a2n=: (+#)@ NB. add 2^n |
||
Line 444: | Line 444: | ||
checkdigit=: INV {~ verhoeff@*&10 |
checkdigit=: INV {~ verhoeff@*&10 |
||
valid=: 0 = verhoeff</ |
valid=: 0 = verhoeff</syntaxhighlight> |
||
Task examples: |
Task examples: |
||
< |
<syntaxhighlight lang="j"> checkdigit 236 12345 123456789012 |
||
3 1 0 |
3 1 0 |
||
valid 2363 |
valid 2363 |
||
Line 477: | Line 477: | ||
3 │5 │4│4 │2│8 │ |
3 │5 │4│4 │2│8 │ |
||
0 │2 │5│5 │1│3 │ |
0 │2 │5│5 │1│3 │ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 483: | Line 483: | ||
{{works with|jq}} |
{{works with|jq}} |
||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
< |
<syntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
||
def d: [ |
def d: [ |
||
Line 556: | Line 556: | ||
"\nThe validation for '\($stc)' is \($v).\n" ); |
"\nThe validation for '\($stc)' is \($v).\n" ); |
||
task</ |
task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
As for [[#Wren]]. |
As for [[#Wren]]. |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">const multiplicationtable = [ |
||
0 1 2 3 4 5 6 7 8 9; |
0 1 2 3 4 5 6 7 8 9; |
||
1 2 3 4 0 6 7 8 9 5; |
1 2 3 4 0 6 7 8 9 5; |
||
Line 615: | Line 615: | ||
verhoeffchecksum(args...) |
verhoeffchecksum(args...) |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}}Same as Wren example. |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
const |
const |
||
Line 699: | Line 699: | ||
discard verhoeff(10 * n + 9, true) |
discard verhoeff(10 * n + 9, true) |
||
except ValueError: |
except ValueError: |
||
echo getCurrentExceptionMsg()</ |
echo getCurrentExceptionMsg()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 761: | Line 761: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Verhoeff_algorithm |
use strict; # https://rosettacode.org/wiki/Verhoeff_algorithm |
||
Line 823: | Line 823: | ||
} |
} |
||
print "\n"; |
print "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 857: | Line 857: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
The tables were generated in case 1-based index versions of them would help, tbh, but in the end I didn't even try that, aka start with tagset(10). |
The tables were generated in case 1-based index versions of them would help, tbh, but in the end I didn't even try that, aka start with tagset(10). |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)},</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)},</span> |
||
Line 919: | Line 919: | ||
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">verhoeff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]&</span><span style="color: #008000;">'9'</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show_workings</span><span style="color: #0000FF;">)!=</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">verhoeff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]&</span><span style="color: #008000;">'9'</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show_workings</span><span style="color: #0000FF;">)!=</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 990: | Line 990: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">MULTIPLICATION_TABLE = [ |
||
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), |
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), |
||
(1, 2, 3, 4, 0, 6, 7, 8, 9, 5), |
(1, 2, 3, 4, 0, 6, 7, 8, 9, 5), |
||
Line 1,049: | Line 1,049: | ||
(1234567890129, True, False, False)]: |
(1234567890129, True, False, False)]: |
||
verhoeffchecksum(n, va, t, ve) |
verhoeffchecksum(n, va, t, ve) |
||
</ |
</syntaxhighlight>{{out}}Output same as Wren example. |
||
Line 1,055: | Line 1,055: | ||
Generate the tables rather than hard coding, They're not all that complex. |
Generate the tables rather than hard coding, They're not all that complex. |
||
<lang |
<syntaxhighlight lang="raku" line>my @d = [^10] xx 5; |
||
@d[$_][^5].=rotate($_), @d[$_][5..*].=rotate($_) for 1..4; |
@d[$_][^5].=rotate($_), @d[$_][5..*].=rotate($_) for 1..4; |
||
push @d: [@d[$_].reverse] for flat 1..4, 0; |
push @d: [@d[$_].reverse] for flat 1..4, 0; |
||
Line 1,101: | Line 1,101: | ||
validate $check, :verbose( $int.chars < 8 ); |
validate $check, :verbose( $int.chars < 8 ); |
||
validate +($check.chop ~ 9), :verbose( $int.chars < 8 ); |
validate +($check.chop ~ 9), :verbose( $int.chars < 8 ); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Checkdigit calculation for 236: |
<pre>Checkdigit calculation for 236: |
||
Line 1,168: | Line 1,168: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="go">const d = [ |
||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], |
||
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5], |
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5], |
||
Line 1,242: | Line 1,242: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,251: | Line 1,251: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var d = [ |
var d = [ |
||
Line 1,306: | Line 1,306: | ||
System.print("\nThe validation for '%(stc)' is %(v ? "correct" : "incorrect").\n") |
System.print("\nThe validation for '%(stc)' is %(v ? "correct" : "incorrect").\n") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |