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-2.0.6.28 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.27 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.26 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.25 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.24 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.23 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.22 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.21 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.20 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.19 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.18 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.17 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.16 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.15 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.14 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.13 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.12 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.11 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.10 tracks/erlang/exercises/luhn/example.erl
trackler-2.0.6.9 tracks/erlang/exercises/luhn/example.erl