Luhn algorithm Learning and its Ruby version implementation code example _ruby topic

Source: Internet
Author: User
Tags valid

About Luhn algorithm
Luhn algorithm, mainly used to calculate the legality of credit card and other document numbers.
1. Starting with the last digit of the card number, the even number is multiplied by 2, and if multiplied by 2 the result is two digits, the number of two digits is added to save.
2, add all the numbers, get the sum.
3. If the credit card number is legal, the sum can be divisible by 10.
Luhn algorithm or Luhn formula, also known as "modulus 10 algorithm." It is a simple validation formula that is typically used for identification numbers, IMEI numbers, U.S. supplier identification numbers, or Canadian social Security numbers. The algorithm was created by IBM scientist Hans Peter Luhn and filed on January 6, 1954 for the patent and was awarded on August 23, 1960, with a patent number of 2950048 in the United States.
This algorithm has been used by everyone, and it has been applied widely today. It is specified in iso/iec7812-1. It is not intended to be a cryptographic security hash function; It is designed to prevent accidental errors, rather than malicious attacks. Many credit cards and numerous government identification numbers use the algorithm to extract valid numbers from a series of random numbers.

Advantages and Disadvantages
The Luhn algorithm detects any single code error and almost all adjacent digit transposition errors. However, it does not detect errors in the two-digit sequence 09 to 90 (and vice versa). It detects seven-tenths of the same double digit error (No 22 and 55 swaps, 33 and 66 swaps, 44 and 77 swaps) are detected. Other more sophisticated algorithms for checking numbers, such as the Ferhof algorithm, can detect more transcription errors. The Luhn algorithm of modulo n is an extension of the Luhn algorithm, which supports non-numeric strings. Because the algorithm takes a right-to-left approach, and 0 bits affect the results of the calculation. The result is generated only if 0 bits cause a digital move or the beginning of a string of digits with zero. Therefore, the results obtained by using the Luhn algorithm are the same regardless of whether the 1234 is filled with 0 before or after 0001234.
The algorithm is used in U.S. patents to compute the parity code for hand-held or mechanical devices. So it has to be as simple as possible.

Ruby Version Implementation
the basic principle of Luhn algorithm is very simple: (eg:49927398716)
The first step: reverse the credit card number (61789372994)
Step Two: Remove the number on the odd position after the reverse, add the sum until S1 (s1=6+7+9+7+9+4=42)
Step three: Remove the number on the even position after the reverse, multiplying each number by 2. (eg:2,16,6,4,18)
Fourth step: The third step to get the number greater than 10 into Single-digit + 10-bit. (eg:2,7,6,4,9)
Fifth step: Add the processed number of even digits, get S2 (s2=2+7+6+4+9=28)
Sixth Step: Interpretation (s1+s2)%10 = = 0 is valid, otherwise invalid. Effective
Code:

Module Luhnvalidator 
 def validate (number) 
  S1 = S2 = 0 
  number.to_s.reverse.chars.each_slice (2) do |odd, even | 
   S1 + + odd.to_i 
 
   double = even.to_i * 2 
   double = 9 if double >= 
   s2 + + double End 
  (s1 + s2)% 10 = = 0? ' Valid ': ' Invalid ' end 
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.