How to resolve the algorithm Luhn test of credit card numbers step by step in the Forth 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 Forth 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 Forth programming language
Source code in the forth programming language
: luhn ( addr len -- ? )
0 >r over + ( R: sum )
begin 1- 2dup <=
while \ odd
dup c@ [char] 0 -
r> + >r
1- 2dup <=
while \ even
dup c@ [char] 0 -
2* 10 /mod + \ even digits doubled, split, and summed
r> + >r
repeat then
2drop r> 10 mod 0= ;
s" 49927398716" luhn . \ -1
s" 49927398717" luhn . \ 0
s" 1234567812345678" luhn . \ 0
s" 1234567812345670" luhn . \ -1
You may also check:How to resolve the algorithm Filter step by step in the Picat programming language
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Honaker primes step by step in the J programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the GAP programming language
You may also check:How to resolve the algorithm Draw a pixel step by step in the Ada programming language