|
1 | 1 | # Instructions |
2 | 2 |
|
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]. |
4 | 4 |
|
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. |
6 | 6 |
|
7 | | -The task is to check if a given string is valid. |
8 | | - |
9 | | -## Validating a Number |
| 7 | +## Validating a number |
10 | 8 |
|
11 | 9 | Strings of length 1 or less are not valid. |
12 | 10 | Spaces are allowed in the input, but they should be stripped before checking. |
13 | 11 | All other non-digit characters are disallowed. |
14 | 12 |
|
15 | | -### Example 1: valid credit card number |
| 13 | +## Examples |
16 | 14 |
|
17 | | -```text |
18 | | -4539 3195 0343 6467 |
19 | | -``` |
| 15 | +### Valid credit card number |
20 | 16 |
|
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. |
23 | 20 |
|
24 | 21 | ```text |
25 | | -4_3_ 3_9_ 0_4_ 6_6_ |
| 22 | +4539 3195 0343 6467 |
| 23 | +↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ (double these) |
26 | 24 | ``` |
27 | 25 |
|
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: |
30 | 28 |
|
31 | 29 | ```text |
32 | 30 | 8569 6195 0383 3437 |
33 | 31 | ``` |
34 | 32 |
|
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. |
36 | 35 |
|
37 | 36 | ```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 |
39 | 38 | ``` |
40 | 39 |
|
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`. |
43 | 45 |
|
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. |
45 | 47 |
|
46 | 48 | ```text |
47 | | -8273 1232 7352 0569 |
| 49 | +066 123 478 |
| 50 | + ↑ ↑ ↑ ↑ (double these) |
48 | 51 | ``` |
49 | 52 |
|
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: |
51 | 55 |
|
52 | 56 | ```text |
53 | | -7253 2262 5312 0539 |
| 57 | +036 226 458 |
54 | 58 | ``` |
55 | 59 |
|
56 | | -Sum the digits |
| 60 | +We sum the digits: |
57 | 61 |
|
58 | 62 | ```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 |
60 | 64 | ``` |
61 | 65 |
|
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! |
63 | 67 |
|
64 | 68 | [luhn]: https://en.wikipedia.org/wiki/Luhn_algorithm |
0 commit comments