How to resolve the algorithm Luhn test of credit card numbers step by step in the TXR 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 TXR 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 TXR programming language

Source code in the txr programming language

@(do (defun luhn (num)
       (for ((i 1) (sum 0))
            ((not (zerop num)) (zerop (mod sum 10)))
            ((inc i) (set num (trunc num 10)))
          (let ((dig (mod num 10)))
            (if (oddp i)
              (inc sum dig)
              (let ((dig2 (* 2 dig)))
                (inc sum (+ (trunc dig2 10) (mod dig2 10)))))))))
@(collect :vars nil)
@{ccnumber /[0-9]+/}
@(output)
@ccnumber -> @(if (luhn (int-str ccnumber 10)) "good" "bad")
@(end)
@(end)

  

You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the 11l programming language
You may also check:How to resolve the algorithm Percolation/Bond percolation step by step in the Python programming language
You may also check:How to resolve the algorithm Product of min and max prime factors step by step in the Ruby programming language
You may also check:How to resolve the algorithm Secure temporary file step by step in the zkl programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the 360 Assembly programming language