Verhoeff algorithm: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 23:
{{trans|Python}}
 
<langsyntaxhighlight 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],
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
Line 77:
(Int64(1234567890120), 1B, 0B, 0B),
(Int64(1234567890129), 1B, 0B, 0B)]
verhoeffchecksum(n, va, t, ve)</langsyntaxhighlight>
 
{{out}}
Line 83:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
Line 147:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 230:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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|]
Line 253:
printfn $"The assertion that 1234567890120 is valid is %A{csum 1234567890120I=0}"
printfn $"The assertion that 1234567890129 is valid is %A{csum 1234567890129I=0}"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 327:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 404:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 415:
Implementation:
 
<langsyntaxhighlight Jlang="j">cyc=: | +/~@i. NB. cyclic group, order y
ac=: |(+-/~@i.) NB. anticyclic group, order y
a2n=: (+#)@ NB. add 2^n
Line 444:
 
checkdigit=: INV {~ verhoeff@*&10
valid=: 0 = verhoeff</langsyntaxhighlight>
 
Task examples:
<langsyntaxhighlight Jlang="j"> checkdigit 236 12345 123456789012
3 1 0
valid 2363
Line 477:
3 │5 │4│4 │2│8 │
0 │2 │5│5 │1│3 │
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
Line 483:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def d: [
Line 556:
"\nThe validation for '\($stc)' is \($v).\n" );
task</langsyntaxhighlight>
{{out}}
As for [[#Wren]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const multiplicationtable = [
0 1 2 3 4 5 6 7 8 9;
1 2 3 4 0 6 7 8 9 5;
Line 615:
verhoeffchecksum(args...)
end
</langsyntaxhighlight>{{out}}Same as Wren example.
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat
 
const
Line 699:
discard verhoeff(10 * n + 9, true)
except ValueError:
echo getCurrentExceptionMsg()</langsyntaxhighlight>
 
{{out}}
Line 761:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Verhoeff_algorithm
Line 823:
}
print "\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 857:
=={{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).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 990:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">MULTIPLICATION_TABLE = [
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
(1, 2, 3, 4, 0, 6, 7, 8, 9, 5),
Line 1,049:
(1234567890129, True, False, False)]:
verhoeffchecksum(n, va, t, ve)
</langsyntaxhighlight>{{out}}Output same as Wren example.
 
 
Line 1,055:
Generate the tables rather than hard coding, They're not all that complex.
 
<syntaxhighlight lang="raku" perl6line>my @d = [^10] xx 5;
@d[$_][^5].=rotate($_), @d[$_][5..*].=rotate($_) for 1..4;
push @d: [@d[$_].reverse] for flat 1..4, 0;
Line 1,101:
validate $check, :verbose( $int.chars < 8 );
validate +($check.chop ~ 9), :verbose( $int.chars < 8 );
}</langsyntaxhighlight>
{{out}}
<pre>Checkdigit calculation for 236:
Line 1,168:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="go">const d = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
Line 1,242:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,251:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var d = [
Line 1,306:
System.print("\nThe validation for '%(stc)' is %(v ? "correct" : "incorrect").\n")
}
}</langsyntaxhighlight>
 
{{out}}
10,327

edits