Sha256: b5b82b1737b4c608c2a6f181193543320cfae2422e106b0b5cea282c635cb0a8

Contents?: true

Size: 769 Bytes

Versions: 84

Compression:

Stored size: 769 Bytes

Contents

-module(luhn).
-export([valid/1, create/1, checksum/1]).



checksum(Number) ->
  checksum(
    lists:reverse(
      lists:filter(
        fun(C) ->
            ($0 =< C) andalso (C =< $9)
        end,
        Number
       )
     ),
    odd,
    0
   ).



checksum([], _, Total) ->
  Total;

checksum([H | ReversedNumber], odd, Total) ->
  checksum(ReversedNumber, even, Total + H - $0);

checksum([H | ReversedNumber], even, Total) when H < $5 ->
  checksum(ReversedNumber, odd, Total + (H - $0) * 2);

checksum([H | ReversedNumber], even, Total) when H >= $5 ->
  checksum(ReversedNumber, odd, Total + ((H - $0) * 2) - 9).



valid(Number) ->
  checksum(Number) rem 10 == 0.



create(Number) ->
  lists:flatten([Number, ($: - (checksum(Number ++ [$0]) rem 10))]).

Version data entries

84 entries across 84 versions & 1 rubygems

Version Path
trackler-1.0.1.1 tracks/erlang/exercises/luhn/example.erl
trackler-1.0.1.0 tracks/erlang/exercises/luhn/example.erl
trackler-1.0.0.1 tracks/erlang/exercises/luhn/example.erl
trackler-1.0.0 tracks/erlang/exercises/luhn/example.erl