X Tutup
Skip to content

Commit f44fdc7

Browse files
committed
added luhn's checksum algorithm
1 parent 31d5467 commit f44fdc7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Checksums/luhn.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function luhn(card_number) {
2+
var sum_of_odds = 0;
3+
var sum_of_evens = 0;
4+
for (var i = 15; i > 0; i-=2) {
5+
sum_of_odds += parseInt(card_number[i]);
6+
}
7+
for (var i = 14; i > -1; i-=2) {
8+
var current_number = parseInt(card_number[i]);
9+
if (parseInt(card_number[i])*2 > 9) {
10+
sum_of_evens += (i*2)-9;
11+
} else {
12+
sum_of_evens += i*2;
13+
}
14+
}
15+
return 0 === ((sum_of_odds + sum_of_evens) % 10);
16+
}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ In cryptography, a **transposition cipher** is a method of encryption by which t
140140
Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt.
141141
###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher)
142142

143+
----------------------------------------------------------------------------------------------------------------------
144+
145+
## Checksums
146+
147+
### Luhn's
148+
The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, 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 and Greek Social Security Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.
149+
###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher)
143150
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
144151
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
145152
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"

0 commit comments

Comments
 (0)
X Tutup