How to resolve the algorithm Luhn test of credit card numbers step by step in the Erlang programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Luhn test of credit card numbers step by step in the Erlang programming language
Table of Contents
Problem Statement
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:
For example, if the trial number is 49927398716:
Write a function/method/procedure/subroutine that will validate a number with the Luhn test, and use it to validate the following numbers:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Luhn test of credit card numbers step by step in the Erlang programming language
Source code in the erlang programming language
-module(luhn_test).
-export( [credit_card/1, task/0] ).
luhn_sum([Odd, Even |Rest]) when Even >= 5 ->
Odd + 2 * Even - 10 + 1 + luhn_sum(Rest);
luhn_sum([Odd, Even |Rest]) ->
Odd + 2 * Even + luhn_sum(Rest);
luhn_sum([Odd]) ->
Odd;
luhn_sum([]) ->
0.
check( Sum ) when (Sum rem 10) =:= 0 -> valid;
check( _Sum ) -> invalid.
credit_card(Digits) ->
check(luhn_sum(lists:map(fun(D) -> D-$0 end, lists:reverse(Digits)))).
task() ->
Numbers = ["49927398716", "49927398717", "1234567812345678", "1234567812345670"],
[io:fwrite("~s: ~p~n", [X, credit_card(X)]) || X <- Numbers].
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the Ada programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Lasso programming language