📜  卢恩算法

📅  最后修改于: 2022-05-13 01:57:08.187000             🧑  作者: Mango

卢恩算法

Luhn 算法,也称为模 10mod 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 公式,该数字有效;否则无效。

由于总和是 70,即 10 的倍数,因此帐号可能有效。

这个想法很简单;我们从末端遍历。对于每第二个数字,我们在添加之前将其加倍。我们将加倍后获得的数字的两位数相加。

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; i--) {
 
        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
// 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.


Python3
# 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


C#
// 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; i--)
    {
 
         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


输出:

This is a valid card

Luhn 算法检测任何一位数字的错误,以及几乎所有相邻数字的换位。