Luhn test of credit card numbers: Difference between revisions

Content added Content deleted
(Added BBC BASIC)
(Simplified D code)
Line 465: Line 465:
<lang d>import std.algorithm;
<lang d>import std.algorithm;


bool luhnTest(const string number) {
pure bool luhnTest(in string num) {
uint len = number.length;
uint sum;
foreach_reverse (i, n; num) {
uint sum;
const uint ord = n - '\u0030';
foreach_reverse(i, n; number) {
uint ord = n - '\u0030';
sum += ((num.length - i) & 1) ? ord : ord / 5 + (2 * ord) % 10;
}
if ((len - i) & 1) {
sum += ord;
return sum % 10 == 0;
} else {
sum += ord / 5 + (2 * ord) % 10;
}
}
return sum % 10 == 0;
}
}


void main() {
unittest {
auto r = map!luhnTest(["49927398716", "49927398717","1234567812345678", "1234567812345670"]);
auto data = ["49927398716", "49927398717",
"1234567812345678", "1234567812345670"];
assert(equal(r, [true, false, false, true]));
assert(equal(map!luhnTest(data), [true, false, false, true]));
}</lang>
}</lang>

=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
<lang fsharp>let luhn (s:string) =
<lang fsharp>let luhn (s:string) =