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

Source code in the elixir programming language

defmodule Luhn do
  def valid?(cc) when is_binary(cc), do: String.to_integer(cc) |> valid?
  def valid?(cc) when is_integer(cc) do
    0 == Integer.digits(cc)
         |> Enum.reverse
         |> Enum.chunk(2, 2, [0])
         |> Enum.reduce(0, fn([odd, even], sum) -> Enum.sum([sum, odd | Integer.digits(even*2)]) end)
         |> rem(10)
  end
end

numbers = ~w(49927398716 49927398717 1234567812345678 1234567812345670)
for n <- numbers, do: IO.puts "#{n}: #{Luhn.valid?(n)}"


  

You may also check:How to resolve the algorithm Quaternion type step by step in the VBA programming language
You may also check:How to resolve the algorithm Chernick's Carmichael numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the Ursa programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the PL/0 programming language
You may also check:How to resolve the algorithm Factorial step by step in the Go programming language