Verhoeff algorithm: Difference between revisions
Content added Content deleted
m (docs) |
(Realize in F#) |
||
Line 19: | Line 19: | ||
* [[Damm algorithm]] |
* [[Damm algorithm]] |
||
<br><br> |
<br><br> |
||
=={{header|F_Sharp|F#}}== |
|||
<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|] |
|||
let p=[|0;1;2;3;4;5;6;7;8;9;1;5;7;6;2;8;3;0;9;4;5;8;0;3;7;9;6;1;4;2;8;9;1;6;0;4;3;5;2;7;9;4;5;3;1;2;6;8;7;0;4;2;8;6;5;7;3;9;0;1;2;7;9;3;8;0;6;4;1;5;7;0;4;6;9;1;3;2;5;8|] |
|||
let inv=[|0;4;3;2;1;5;6;7;8;9|] in (fun n g->d.[10*n+g]),(fun g->inv.[g]),(fun n g->p.[10*(n%8)+g]) |
|||
let fN g=Seq.unfold(fun(i,g,l)->if i=0I then None else let ni=int(i%10I) in let l=d l (p g ni) in Some((ni,l),(i/10I,g+1,l)))(g,0,0) |
|||
let csum g=let _,g=Seq.last(fN g) in inv g |
|||
let printTable g=printfn $"Work Table for %A{g}\n i nᵢ p[i,nᵢ] c\n--------------"; fN g|>Seq.iteri(fun i (n,g)->printfn $"%d{i} %d{n} %d{p i n} %d{g}") |
|||
printTable 2360I |
|||
printfn $"\nThe CheckDigit for 236 is %d{csum 2360I}\n" |
|||
printTable 2363I |
|||
printfn $"\nThe assertion that 2363 is valid is %A{csum 2363I=0}\n" |
|||
printTable 2369I |
|||
printfn $"\nThe assertion that 2369 is valid is %A{csum 2369I=0}\n" |
|||
printTable 123450I |
|||
printfn $"\nThe CheckDigit for 12345 is %d{csum 123450I}\n" |
|||
printTable 123451I |
|||
printfn $"\nThe assertion that 123451 is valid is %A{csum 123451I=0}\n" |
|||
printTable 123459I |
|||
printfn $"\nThe assertion that 123459 is valid is %A{csum 123459I=0}" |
|||
printfn $"The CheckDigit for 123456789012 is %d{csum 1234567890120I}" |
|||
printfn $"The assertion that 1234567890120 is valid is %A{csum 1234567890120I=0}" |
|||
printfn $"The assertion that 1234567890129 is valid is %A{csum 1234567890129I=0}" |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Work Table for 2360 |
|||
i nᵢ p[i,nᵢ] c |
|||
-------------- |
|||
0 0 0 0 |
|||
1 6 3 3 |
|||
2 3 3 1 |
|||
3 2 1 2 |
|||
The CheckDigit for 236 is 3 |
|||
Work Table for 2363 |
|||
i nᵢ p[i,nᵢ] c |
|||
-------------- |
|||
0 3 3 3 |
|||
1 6 3 1 |
|||
2 3 3 4 |
|||
3 2 1 0 |
|||
The assertion that 2363 is valid is true |
|||
Work Table for 2369 |
|||
i nᵢ p[i,nᵢ] c |
|||
-------------- |
|||
0 9 9 9 |
|||
1 6 3 6 |
|||
2 3 3 8 |
|||
3 2 1 7 |
|||
The assertion that 2369 is valid is false |
|||
Work Table for 123450 |
|||
i nᵢ p[i,nᵢ] c |
|||
-------------- |
|||
0 0 0 0 |
|||
1 5 8 8 |
|||
2 4 7 1 |
|||
3 3 6 7 |
|||
4 2 5 2 |
|||
5 1 2 4 |
|||
The CheckDigit for 12345 is 1 |
|||
Work Table for 123451 |
|||
i nᵢ p[i,nᵢ] c |
|||
-------------- |
|||
0 1 1 1 |
|||
1 5 8 9 |
|||
2 4 7 2 |
|||
3 3 6 8 |
|||
4 2 5 3 |
|||
5 1 2 0 |
|||
The assertion that 123451 is valid is true |
|||
Work Table for 123459 |
|||
i nᵢ p[i,nᵢ] c |
|||
-------------- |
|||
0 9 9 9 |
|||
1 5 8 1 |
|||
2 4 7 8 |
|||
3 3 6 2 |
|||
4 2 5 7 |
|||
5 1 2 5 |
|||
The assertion that 123459 is valid is false |
|||
The CheckDigit for 123456789012 is 0 |
|||
The assertion that 1234567890120 is valid is true |
|||
The assertion that 1234567890129 is valid is false |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |