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