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( |
pure bool luhnTest(in string num) { |
||
uint sum; |
|||
foreach_reverse (i, n; num) { |
|||
uint sum; |
|||
const uint ord = n - '\u0030'; |
|||
foreach_reverse(i, n; number) { |
|||
sum += ((num.length - i) & 1) ? ord : ord / 5 + (2 * ord) % 10; |
|||
⚫ | |||
if ((len - i) & 1) { |
|||
return sum % 10 == 0; |
|||
} else { |
|||
sum += ord / 5 + (2 * ord) % 10; |
|||
} |
|||
⚫ | |||
return sum % 10 == 0; |
|||
} |
} |
||
void main() { |
|||
unittest { |
|||
auto data = ["49927398716", "49927398717", |
|||
"1234567812345678", "1234567812345670"]; |
|||
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) = |