i--)
int temp = digits[i]; if(alt)
temp *= 2; if(temp > 9)
temp -= 9; //równoważne dodaniu cyfr do siebie np. 1+6 = 7, 16-9 = 7
sum += temp; alt = !alt;
return sum % 10 == 0;
Następująca funkcja (w C#) generuje losową liczbę zakończoną cyfrą kontrolną obliczoną przy pomocy algorytmu Luhna. int[] CreateNumber(int length)
Random random = new Random(); int[] digits = new int[length]; for(int i = 0; i < length - 1; i++)
digits[i] = random.Next(10);
int sum = 0; bool alt = true; for(int i = length - 2; i >= 0; i--)
int temp = digits[i]; if(alt)
temp *= 2; if(temp > 9)
temp -= 9; //równoważne dodaniu cyfr do siebie np. 1+6 = 7, 16-9 = 7
sum += temp; alt = !alt;
int modulo = sum % 10; if(modulo > 0)
modulo = 10 - modulo;
digits[length-1] = modulo; return digits;
Linki zewnętrzne Patent Luhna Nr 2950048 Computer for Verifying Numbers, Hans P. Luhn, August 23, 1960. Weryfikacja i obliczanie cyfry kontrolnej Luhna ISNI, PIC, LEI, ORCID, ISTC, ISO-7064.
Kategoria:Integralność danych