Luhn test of credit card numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Excel}}: Added an Excel LAMBDA version.)
Line 2,418: Line 2,418:
1234567812345678 = 0
1234567812345678 = 0
1234567812345670 = 1</pre>
1234567812345670 = 1</pre>

=={{header|Excel}}==
===LAMBDA===

Binding the name '''luhnChecked''' to the following lambda expression in the Name Manager of the Excel WorkBook:

(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])

{{Works with|Office 365 betas 2021}}
<lang lisp>luhnChecked
=LAMBDA(s,
LET(
ns, REVERSECOLS(VALUE(CHARSROW(s))),
ixs, SEQUENCE(1, COLUMNS(ns), 1, 1),

0 = MOD(SUM(
FILTER(ns, 0 <> MOD(ixs, 2))
) + (
LAMBDA(n,
DIGITSUM(
CONCAT(TEXT(2 * n, "0"))
)
)(
FILTER(ns, 0 = MOD(ixs, 2))
)
),
10
)
)
)</lang>

and also assuming the following generic bindings in the Name Manager for the WorkBook:

<lang lisp>CHARSROW
=LAMBDA(s,
MID(s,
SEQUENCE(1, LEN(s), 1, 1),
1
)
)


DIGITSUM
=LAMBDA(s,
SUM(VALUE(
MID(s,
SEQUENCE(LEN(s), 1, 1, 1),
1
)
))
)


REVERSECOLS
=LAMBDA(xs,
LET(
n, COLUMNS(xs),

SORTBY(
xs,
SEQUENCE(1, n, n, -1)
)
)
)</lang>

{{Out}}
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="2" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=luhnChecked(A2)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| style="font-weight:bold" | Digit strings
| style="font-weight:bold" | Luhn result
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:right" | 49927398716
| style="background-color:#cbcefb" | TRUE
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
| style="text-align:right" | 49927398717
| FALSE
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
| style="text-align:right" | 1234567812345678
| FALSE
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
| style="text-align:right" | 1234567812345670
| TRUE
|}


=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==