Damm algorithm: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(add PicoLisp)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 144:
check digit of 5727 is invalid
check digit of 112946 is valid</pre>
 
=={{header|AWK}}==
<lang AWK># syntax: GAWK -f DAMM_ALGORITHM.AWK
Line 212 ⟶ 213:
return 0;
}</lang>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<lang csharp>using System;
 
namespace DammAlgorithm {
class Program {
static int[,] 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},
{6, 1, 2, 3, 0, 4, 5, 9, 7, 8},
{3, 6, 7, 4, 2, 0, 9, 5, 8, 1},
{5, 8, 6, 9, 7, 2, 0, 1, 3, 4},
{8, 9, 4, 5, 3, 6, 2, 0, 1, 7},
{9, 4, 3, 8, 6, 1, 7, 2, 0, 5},
{2, 5, 8, 1, 4, 3, 6, 7, 9, 0},
};
 
static bool Damm(string s) {
int interim = 0;
foreach (char c in s) {
interim = table[interim, c - '0'];
}
return interim == 0;
}
 
static void Main(string[] args) {
int[] numbers = { 5724, 5727, 112946, 112949 };
foreach (int number in numbers) {
bool isValid = Damm(number.ToString());
if (isValid) {
Console.WriteLine("{0,6} is valid", number);
}
else {
Console.WriteLine("{0,6} is invalid", number);
}
}
}
}
}</lang>
{{out}}
<pre> 5724 is valid
5727 is invalid
112946 is valid
112949 is invalid</pre>
 
=={{header|C++}}==
Line 254 ⟶ 302:
 
return 0;
}</lang>
{{out}}
<pre> 5724 is valid
5727 is invalid
112946 is valid
112949 is invalid</pre>
 
=={{header|C#|C sharp}}==
{{trans|Java}}
<lang csharp>using System;
 
namespace DammAlgorithm {
class Program {
static int[,] 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},
{6, 1, 2, 3, 0, 4, 5, 9, 7, 8},
{3, 6, 7, 4, 2, 0, 9, 5, 8, 1},
{5, 8, 6, 9, 7, 2, 0, 1, 3, 4},
{8, 9, 4, 5, 3, 6, 2, 0, 1, 7},
{9, 4, 3, 8, 6, 1, 7, 2, 0, 5},
{2, 5, 8, 1, 4, 3, 6, 7, 9, 0},
};
 
static bool Damm(string s) {
int interim = 0;
foreach (char c in s) {
interim = table[interim, c - '0'];
}
return interim == 0;
}
 
static void Main(string[] args) {
int[] numbers = { 5724, 5727, 112946, 112949 };
foreach (int number in numbers) {
bool isValid = Damm(number.ToString());
if (isValid) {
Console.WriteLine("{0,6} is valid", number);
}
else {
Console.WriteLine("{0,6} is invalid", number);
}
}
}
}
}</lang>
{{out}}
Line 443 ⟶ 444:
112949 is invalid
</pre>
 
=={{header|Fōrmulæ}}==
 
In [https://wiki.formulae.org/Damm_algorithm this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Fortran}}==
Line 548 ⟶ 541:
Checksum test: 5727 is invalid
Checksum test: 112946 is valid</pre>
 
=={{header|Fōrmulæ}}==
 
In [https://wiki.formulae.org/Damm_algorithm this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
Line 943 ⟶ 944:
for (5724, 5727, 112946) {
print "$_:\tChecksum digit @{[damm($_) ? '' : 'in']}correct.\n"
}</lang>
{{out}}
<pre>5724: Checksum digit correct.
5727: Checksum digit incorrect.
112946: Checksum digit correct.</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2017.05}}
 
<lang perl6>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],
[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, 1],
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0];
my $row = 0;
for @digits -> $col { $row = @tbl[$row][$col] }
not $row
}
 
# Testing
for 5724, 5727, 112946 {
say "$_:\tChecksum digit { damm( $_.comb ) ?? '' !! 'in' }correct."
}</lang>
{{out}}
Line 1,116 ⟶ 1,089:
(check-true (valid-number? 112946)))</lang>
No output from checks means that all tests passed.
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2017.05}}
 
<lang perl6>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],
[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, 1],
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0];
my $row = 0;
for @digits -> $col { $row = @tbl[$row][$col] }
not $row
}
 
# Testing
for 5724, 5727, 112946 {
say "$_:\tChecksum digit { damm( $_.comb ) ?? '' !! 'in' }correct."
}</lang>
{{out}}
<pre>5724: Checksum digit correct.
5727: Checksum digit incorrect.
112946: Checksum digit correct.</pre>
 
=={{header|REXX}}==
Line 1,336 ⟶ 1,338:
}</lang>
{{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}}==
<lang ruby>func damm(digits) {
10,339

edits