Luhn test of credit card numbers: Difference between revisions

Content deleted Content added
→‎{{header|Octave}}: change int2str to mat2str because mat2str supports larger numbers
Updated D versions
Line 464:
<lang d>import std.algorithm;
 
pure bool luhnTest(in string num) @safe pure nothrow {
uint sum;
foreach_reverse (i, n; num) {
constimmutable uint ord = n - '\u0030';
sum += ((num.length - i) & 1) ? ord : ord / 5 + (2 * ord) % 10;
}
Line 474:
 
void main() {
autoimmutable data = ["49927398716", "49927398717",
"1234567812345678", "1234567812345670"];
assert(equal(map!luhnTest(data), [true, false, false, true]));
}</lang>
Line 483:
<lang d>import std.stdio;
 
constimmutable struct Interval(T) {
T a, b;
 
Line 493:
bool opBinaryRight(string op="in")(in T x) pure nothrow {
return x >= a && x <= b;
}
 
pure nothrow invariant() {
assert(a <= b);
}
}