Skip to content

Instantly share code, notes, and snippets.

@systra
Last active August 29, 2015 14:19
Show Gist options
  • Save systra/d11d26e056649d978001 to your computer and use it in GitHub Desktop.
Save systra/d11d26e056649d978001 to your computer and use it in GitHub Desktop.
The calculation of the checksum digit of the IMEI number according to Luhn formula.
-module(imei).
-export([crc/1]).
crc(IMEI) when is_list(IMEI), length(IMEI) =:= 14 ->
calc(IMEI, 0).
calc([], Acc) ->
(10 - (Acc rem 10)) rem 10;
calc(L = [X|Rest], Acc) when length(L) rem 2 =:= 0 ->
calc(Rest, Acc + X - $0);
calc([X|Rest], Acc) ->
S = lists:sum([list_to_integer([Y]) || Y <- integer_to_list((X - $0) * 2)]),
calc(Rest, Acc + S).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment