Last active
August 29, 2015 14:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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