Luhn 알고리즘이라고도 함 모듈러스 10 또는 10을 향해 알고리즘은 신용 카드 번호, IMEI 번호, 캐나다 사회 보험 번호와 같은 다양한 식별 번호를 확인하는 데 사용되는 간단한 체크섬 공식입니다. LUHN 공식은 1960년대 후반 수학자 그룹에 의해 만들어졌습니다. 얼마 지나지 않아 신용카드사들이 이를 채택했다. 알고리즘은 공개 도메인에 있으므로 누구나 사용할 수 있습니다. 대부분의 신용 카드와 많은 정부 식별 번호는 유효한 숫자와 잘못 입력되었거나 잘못된 숫자를 구별하는 간단한 방법으로 알고리즘을 사용합니다. 악의적인 공격이 아닌 우발적인 오류로부터 보호하도록 설계되었습니다.
Luhn 알고리즘과 관련된 단계
예를 들어 알고리즘을 이해해 보겠습니다.
계좌번호의 예를 생각해 보세요. 79927398713 .
1 단계 – 가장 오른쪽 숫자부터 시작하여 매 두 번째 숫자의 값을 두 배로 늘립니다.
2 단계 – 숫자를 두 배로 늘려 두 자리 숫자가 되는 경우, 즉 9보다 큰 경우(예: 6 × 2 = 12) 곱의 숫자를 더합니다(예: 12: 1 + 2 = 3, 15: 1 + 5 = 6) 한 자리 숫자를 얻으려면.
3단계 – 이제 모든 숫자의 합을 구합니다.
이진 트리 유형
4단계 – 총 모듈로 10이 0과 같으면(총계가 0으로 끝나는 경우) 숫자는 Luhn 공식에 따라 유효합니다. 그렇지 않으면 유효하지 않습니다.
합이 10의 배수인 70이므로 계좌번호가 유효할 가능성이 높습니다.
아이디어는 간단합니다. 우리는 끝에서 횡단합니다. 두 번째 숫자마다 추가하기 전에 두 배로 늘립니다. 두 배로 늘린 후 얻은 숫자의 두 자리를 더합니다.
구현:
C++
// C++ program to implement Luhn algorithm> #include> using> namespace> std;> // Returns true if given card number is valid> bool> checkLuhn(> const> string& cardNo)> {> > int> nDigits = cardNo.length();> > int> nSum = 0, isSecond => false> ;> > for> (> int> i = nDigits - 1; i>= 0; 나--) {> > int> d = cardNo[i] -> '0'> ;> > if> (isSecond ==> true> )> > d = d * 2;> > // We add two digits to handle> > // cases that make two digits after> > // doubling> > nSum += d / 10;> > nSum += d % 10;> > isSecond = !isSecond;> > }> > return> (nSum % 10 == 0);> }> // Driver code> int> main()> {> > string cardNo => '79927398713'> ;> > if> (checkLuhn(cardNo))> > printf> (> 'This is a valid card'> );> > else> > printf> (> 'This is not a valid card'> );> > return> 0;> }> |
>
>
자바
// Java program to implement> // Luhn algorithm> import> java.io.*;> class> GFG {> > // Returns true if given> // card number is valid> static> boolean> checkLuhn(String cardNo)> {> > int> nDigits = cardNo.length();> > int> nSum => 0> ;> > boolean> isSecond => false> ;> > for> (> int> i = nDigits -> 1> ; i>=> 0> ; i--)> > {> > int> d = cardNo.charAt(i) -> '0'> ;> > if> (isSecond ==> true> )> > d = d *> 2> ;> > // We add two digits to handle> > // cases that make two digits> > // after doubling> > nSum += d /> 10> ;> > nSum += d %> 10> ;> > isSecond = !isSecond;> > }> > return> (nSum %> 10> ==> 0> );> }> > // Driver code> > static> public> void> main (String[] args)> > {> > String cardNo => '79927398713'> ;> > if> (checkLuhn(cardNo))> > System.out.println(> 'This is a valid card'> );> > else> > System.out.println(> 'This is not a valid card'> );> > > }> }> // This Code is contributed by vt_m.> |
의도 의도
>
>
파이썬3
# Python3 program to implement> # Luhn algorithm> # Returns true if given card> # number is valid> def> checkLuhn(cardNo):> > > nDigits> => len> (cardNo)> > nSum> => 0> > isSecond> => False> > > for> i> in> range> (nDigits> -> 1> ,> -> 1> ,> -> 1> ):> > d> => ord> (cardNo[i])> -> ord> (> '0'> )> > > if> (isSecond> => => True> ):> > d> => d> *> 2> > > # We add two digits to handle> > # cases that make two digits after> > # doubling> > nSum> +> => d> /> /> 10> > nSum> +> => d> %> 10> > > isSecond> => not> isSecond> > > if> (nSum> %> 10> => => 0> ):> > return> True> > else> :> > return> False> # Driver code> if> __name__> => => '__main__'> :> > > cardNo> => '79927398713'> > > if> (checkLuhn(cardNo)):> > print> (> 'This is a valid card'> )> > else> :> > print> (> 'This is not a valid card'> )> # This code is contributed by rutvik_56> |
>
>
씨#
nfa에서 dfa로
// C# program to implement> // Luhn algorithm> using> System;> class> GFG {> > // Returns true if given> // card number is valid> static> bool> checkLuhn(String cardNo)> {> > int> nDigits = cardNo.Length;> > int> nSum = 0;> > bool> isSecond => false> ;> > for> (> int> i = nDigits - 1; i>= 0; 나--)> > {> > int> d = cardNo[i] -> '0'> ;> > if> (isSecond ==> true> )> > d = d * 2;> > // We add two digits to handle> > // cases that make two digits> > // after doubling> > nSum += d / 10;> > nSum += d % 10;> > isSecond = !isSecond;> > }> > return> (nSum % 10 == 0);> }> > // Driver code> > static> public> void> Main()> > {> > String cardNo => '79927398713'> ;> > if> (checkLuhn(cardNo))> > Console.WriteLine(> 'This is a valid card'> );> > else> > Console.WriteLine(> 'This is not a valid card'> );> > > }> }> // This Code is contributed by vt_m.> |
>
>
자바스크립트
> > // Javascript program to implement Luhn algorithm> > > // Returns true if given> > // card number is valid> > function> checkLuhn(cardNo)> > {> > let nDigits = cardNo.length;> > let nSum = 0;> > let isSecond => false> ;> > for> (let i = nDigits - 1; i>= 0; 나--)> > {> > let d = cardNo[i].charCodeAt() -> '0'> .charCodeAt();> > if> (isSecond ==> true> )> > d = d * 2;> > // We add two digits to handle> > // cases that make two digits> > // after doubling> > nSum += parseInt(d / 10, 10);> > nSum += d % 10;> > isSecond = !isSecond;> > }> > return> (nSum % 10 == 0);> > }> > > let cardNo => '79927398713'> ;> > if> (checkLuhn(cardNo))> > document.write(> 'This is a valid card'> );> > else> > document.write(> 'This is not a valid card'> );> > > |
피트 데이비슨은 몇 살입니까?
>
>산출
This is a valid card>
Luhn 알고리즘은 한 자리 숫자의 오류는 물론 인접한 숫자의 거의 모든 전치도 감지합니다.