Luhn test of credit card numbers: Difference between revisions
Content added Content deleted
(→{{header|TXR}}: Added.) |
(Luhn test for Octave and Matlab) |
||
Line 1,205: | Line 1,205: | ||
<lang ocaml># List.map luhn [ "49927398716"; "49927398717"; "1234567812345678"; "1234567812345670" ];; |
<lang ocaml># List.map luhn [ "49927398716"; "49927398717"; "1234567812345678"; "1234567812345670" ];; |
||
- : bool list = [true; false; false; true]</lang> |
- : bool list = [true; false; false; true]</lang> |
||
=={{header|Octave}}== |
|||
<lang Octave> function y = isluhn(s); |
|||
if isnumeric(s) s = int2str(s); end; % make sure s is a string |
|||
d = s-'0'; % convert string into vector of digits |
|||
m = [2:2:8,1:2:9]; % rule 3: maps [1:9] -> i |
|||
y = ~mod(sum(d(end:-2:1)) + sum(m(d(end-1:-2:1))),10); |
|||
end; </lang> |
|||
Sample output |
|||
<lang octave> isluhn('49927398716') |
|||
ans = 1 |
|||
isluhn('49927398717') |
|||
ans = 0 |
|||
isluhn('1234567812345678') |
|||
ans = 0 |
|||
isluhn('1234567812345670') |
|||
ans = 1 |
|||
</lang> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |