Skip to content

Commit 7d2d7e5

Browse files
committed
Sync luhn docs
1 parent 8d20737 commit 7d2d7e5

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed
Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,68 @@
11
# Instructions
22

3-
Given a number determine whether or not it is valid per the Luhn formula.
3+
Determine whether a number is valid according to the [Luhn formula][luhn].
44

5-
The [Luhn algorithm][luhn] is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers and Canadian Social Insurance Numbers.
5+
The number will be provided as a string.
66

7-
The task is to check if a given string is valid.
8-
9-
## Validating a Number
7+
## Validating a number
108

119
Strings of length 1 or less are not valid.
1210
Spaces are allowed in the input, but they should be stripped before checking.
1311
All other non-digit characters are disallowed.
1412

15-
### Example 1: valid credit card number
13+
## Examples
1614

17-
```text
18-
4539 3195 0343 6467
19-
```
15+
### Valid credit card number
2016

21-
The first step of the Luhn algorithm is to double every second digit, starting from the right.
22-
We will be doubling
17+
The number to be checked is `4539 3195 0343 6467`.
18+
19+
The first step of the Luhn algorithm is to start at the end of the number and double every second digit, beginning with the second digit from the right and moving left.
2320

2421
```text
25-
4_3_ 3_9_ 0_4_ 6_6_
22+
4539 3195 0343 6467
23+
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ (double these)
2624
```
2725

28-
If doubling the number results in a number greater than 9 then subtract 9 from the product.
29-
The results of our doubling:
26+
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
27+
We end up with:
3028

3129
```text
3230
8569 6195 0383 3437
3331
```
3432

35-
Then sum all of the digits:
33+
Finally, we sum all digits.
34+
If the sum is evenly divisible by 10, the original number is valid.
3635

3736
```text
38-
8+5+6+9+6+1+9+5+0+3+8+3+3+4+3+7 = 80
37+
8 + 5 + 6 + 9 + 6 + 1 + 9 + 5 + 0 + 3 + 8 + 3 + 3 + 4 + 3 + 7 = 80
3938
```
4039

41-
If the sum is evenly divisible by 10, then the number is valid.
42-
This number is valid!
40+
80 is evenly divisible by 10, so number `4539 3195 0343 6467` is valid!
41+
42+
### Invalid Canadian SIN
43+
44+
The number to be checked is `066 123 468`.
4345

44-
### Example 2: invalid credit card number
46+
We start at the end of the number and double every second digit, beginning with the second digit from the right and moving left.
4547

4648
```text
47-
8273 1232 7352 0569
49+
066 123 478
50+
↑ ↑ ↑ ↑ (double these)
4851
```
4952

50-
Double the second digits, starting from the right
53+
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
54+
We end up with:
5155

5256
```text
53-
7253 2262 5312 0539
57+
036 226 458
5458
```
5559

56-
Sum the digits
60+
We sum the digits:
5761

5862
```text
59-
7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
63+
0 + 3 + 6 + 2 + 2 + 6 + 4 + 5 + 8 = 36
6064
```
6165

62-
57 is not evenly divisible by 10, so this number is not valid.
66+
36 is not evenly divisible by 10, so number `066 123 478` is not valid!
6367

6468
[luhn]: https://en.wikipedia.org/wiki/Luhn_algorithm
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Introduction
2+
3+
At the Global Verification Authority, you've just been entrusted with a critical assignment.
4+
Across the city, from online purchases to secure logins, countless operations rely on the accuracy of numerical identifiers like credit card numbers, bank account numbers, transaction codes, and tracking IDs.
5+
The Luhn algorithm is a simple checksum formula used to help identify mistyped numbers.
6+
7+
A batch of identifiers has just arrived on your desk.
8+
All of them must pass the Luhn test to ensure they're legitimate.
9+
If any fail, they'll be flagged as invalid, preventing mistakes such as incorrect transactions or failed account verifications.
10+
11+
Can you ensure this is done right? The integrity of many services depends on you.

0 commit comments

Comments
 (0)