mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Merge 8baabf38c5
into ca3d16dcce
This commit is contained in:
commit
99771f6578
41
src/algorithms/uncategorized/luhn-algorithm/README.md
Normal file
41
src/algorithms/uncategorized/luhn-algorithm/README.md
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
The **Luhn algorithm** or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, named after its creator, IBM scientist Hans Peter Luhn, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers, Greek Social Security Numbers (ΑΜΚΑ), and survey codes appearing on McDonald's, Taco Bell, and Tractor Supply Co. receipts. It is described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.
|
||||||
|
|
||||||
|
The algorithm is in the public domain and is in wide use today. It is specified in ISO/IEC 7812-1.[1] It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers.
|
||||||
|
|
||||||
|
The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This number must pass the following test:
|
||||||
|
|
||||||
|
1. From the rightmost digit, which is the check digit, and moving left, double the value of every second digit. The check digit is not doubled; the first digit doubled is immediately to the left of the check digit. If the result of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then add the digits of the result (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9) or, alternatively, the same final result can be found by subtracting 9 from that result (e.g., 16: 16 − 9 = 7, 18: 18 − 9 = 9).
|
||||||
|
2. Take the sum of all the digits.
|
||||||
|
3. If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; otherwise it is not valid.
|
||||||
|
|
||||||
|
Assume an example of an account number "7992739871" that will have a check digit added, making it of the form 7992739871x:
|
||||||
|
|
||||||
|
The sum of all the digits in the third row is 67+x.
|
||||||
|
|
||||||
|
The check digit (x) is obtained by computing the sum of the non-check digits then computing 9 times that value modulo 10 (in equation form, ((67 × 9) mod 10)). In algorithm form:
|
||||||
|
|
||||||
|
1. Compute the sum of the non-check digits (67).
|
||||||
|
2. Multiply by 9 (603).
|
||||||
|
3. The units digit (3) is the check digit. Thus, x=3.
|
||||||
|
|
||||||
|
(Alternative method) The check digit (x) is obtained by computing the sum of the other digits (third row) then subtracting the units digit from 10 (67 => Units digit 7; 10 − 7 = check digit 3). In algorithm form:
|
||||||
|
|
||||||
|
1. Compute the sum of the non-check digits (67).
|
||||||
|
2. Take the units digit (7).
|
||||||
|
3. Subtract the units digit from 10.
|
||||||
|
T4. he result (3) is the check digit. In case the sum of digits ends in 0 then 0 is the check digit.
|
||||||
|
|
||||||
|
This makes the full account number read 79927398713.
|
||||||
|
|
||||||
|
Each of the numbers 79927398710, 79927398711, 79927398712, 79927398713, 79927398714, 79927398715, 79927398716, 79927398717, 79927398718, 79927398719 can be validated as follows.
|
||||||
|
|
||||||
|
1. Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
|
||||||
|
2. Sum all the individual digits (digits in parentheses are the products from Step 1): x (the check digit) + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 7 = x + 67.
|
||||||
|
3. If the sum is a multiple of 10, the account number is possibly valid. Note that 3 is the only valid digit that produces a sum (70) that is a multiple of 10.
|
||||||
|
4. Thus these account numbers are all invalid except possibly 79927398713 which has the correct check digit.
|
||||||
|
|
||||||
|
Alternately, you can use the same checksum creation algorithm, ignoring the checksum already in place as if it had not yet been calculated. Then calculate the checksum and compare this calculated checksum to the original checksum included with the credit card number. If the included checksum matches the calculated checksum, then the number is valid.
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
- [Wikipedia](https://en.wikipedia.org/wiki/Luhn_algorithm)
|
@ -0,0 +1,103 @@
|
|||||||
|
import luhnAlgorithm from '../luhnAlgorithm';
|
||||||
|
|
||||||
|
describe('luhnAlgorithm', () => {
|
||||||
|
it('it works for single digit numbers', () => {
|
||||||
|
expect(luhnAlgorithm(0)).toBe(true);
|
||||||
|
expect(luhnAlgorithm(1)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(2)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(3)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(4)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(5)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(6)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(7)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(8)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(9)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it works for double digit numbers', () => {
|
||||||
|
expect(luhnAlgorithm(40)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(41)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(42)).toBe(true);
|
||||||
|
expect(luhnAlgorithm(43)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(44)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(45)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(46)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(47)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(48)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it works for longer odd numbers', () => {
|
||||||
|
expect(luhnAlgorithm(49927398710)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49927398711)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49927398712)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49927398713)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49927398714)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49927398715)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49927398716)).toBe(true);
|
||||||
|
expect(luhnAlgorithm(49927398717)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49927398718)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(49927398719)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it works for longer even numbers', () => {
|
||||||
|
expect(luhnAlgorithm(1234567812345670)).toBe(true);
|
||||||
|
expect(luhnAlgorithm(12345678123456701)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(12345678123456702)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(12345678123456703)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(12345678123456704)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(12345678123456705)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(12345678123456706)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(12345678123456707)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(12345678123456708)).toBe(false);
|
||||||
|
expect(luhnAlgorithm(12345678123456709)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('luhnAlgorithm', () => {
|
||||||
|
it('it works for strings', () => {
|
||||||
|
expect(luhnAlgorithm('0')).toBe(true);
|
||||||
|
expect(luhnAlgorithm('1')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('2')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('3')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('4')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('5')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('6')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('7')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('8')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('9')).toBe(false);
|
||||||
|
|
||||||
|
expect(luhnAlgorithm('40')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('41')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('42')).toBe(true);
|
||||||
|
expect(luhnAlgorithm('43')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('44')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('45')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('46')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('47')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('48')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49')).toBe(false);
|
||||||
|
|
||||||
|
expect(luhnAlgorithm('49927398710')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49927398711')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49927398712')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49927398713')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49927398714')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49927398715')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49927398716')).toBe(true);
|
||||||
|
expect(luhnAlgorithm('49927398717')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49927398718')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('49927398719')).toBe(false);
|
||||||
|
|
||||||
|
expect(luhnAlgorithm('1234567812345670')).toBe(true);
|
||||||
|
expect(luhnAlgorithm('12345678123456701')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('12345678123456702')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('12345678123456703')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('12345678123456704')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('12345678123456705')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('12345678123456706')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('12345678123456707')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('12345678123456708')).toBe(false);
|
||||||
|
expect(luhnAlgorithm('12345678123456709')).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
43
src/algorithms/uncategorized/luhn-algorithm/luhnAlgorithm.js
Normal file
43
src/algorithms/uncategorized/luhn-algorithm/luhnAlgorithm.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Returns true/false depending on input account number
|
||||||
|
*
|
||||||
|
* @param {string\number} accountNumber
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
|
export default function luhnAlgorithm(accountNumber) {
|
||||||
|
// create two variables s1 and s2 for the odd digit and even digit sums.
|
||||||
|
let s1 = 0;
|
||||||
|
let s2 = 0;
|
||||||
|
|
||||||
|
// create variable to decrement accountNumber
|
||||||
|
let accountNo = accountNumber;
|
||||||
|
|
||||||
|
// create flag to toggle between oddDigit and evenDigit
|
||||||
|
let isOddDigit = true;
|
||||||
|
|
||||||
|
|
||||||
|
while (accountNo > 0) {
|
||||||
|
// use mod 10 to get next digit
|
||||||
|
const nextDigit = accountNo % 10;
|
||||||
|
|
||||||
|
// decrease accountNo by dividing it by 10
|
||||||
|
accountNo = Math.floor(accountNo / 10);
|
||||||
|
|
||||||
|
// if oddDigit, add nextDigit to s1
|
||||||
|
if (isOddDigit) {
|
||||||
|
s1 += nextDigit;
|
||||||
|
} else {
|
||||||
|
// else, double nextDigit
|
||||||
|
const twiceDigit = nextDigit * 2;
|
||||||
|
|
||||||
|
// add the sum of twiceDigit's digits to s2
|
||||||
|
s2 += (twiceDigit % 10) + Math.floor(twiceDigit / 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// toggle isOddDigit flag
|
||||||
|
isOddDigit = !isOddDigit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if s1 + s2 mod 10 is 0, the accountNumber is valid
|
||||||
|
return (s1 + s2) % 10 === 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user