You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A shipment of emergency supplies has arrived, but there's a problem.
4
+
To protect from damage, the items — flashlights, first-aid kits, blankets — are packed inside boxes, and some of those boxes are nested several layers deep inside other boxes!
5
+
6
+
To be prepared for an emergency, everything must be easily accessible in one box.
7
+
Can you unpack all the supplies and place them into a single box, so they're ready when needed most?
Determine whether a credit card number is valid according to the [Luhn formula][luhn].
3
+
Determine whether a number is valid according to the [Luhn formula][luhn].
4
4
5
5
The number will be provided as a string.
6
6
@@ -10,54 +10,59 @@ Strings of length 1 or less are not valid.
10
10
Spaces are allowed in the input, but they should be stripped before checking.
11
11
All other non-digit characters are disallowed.
12
12
13
-
### Example 1: valid credit card number
13
+
##Examples
14
14
15
-
```text
16
-
4539 3195 0343 6467
17
-
```
15
+
### Valid credit card number
18
16
19
-
The first step of the Luhn algorithm is to double every second digit, starting from the right.
20
-
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.
21
20
22
21
```text
23
22
4539 3195 0343 6467
24
23
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ (double these)
25
24
```
26
25
27
-
If doubling the number results in a number greater than 9 then subtract 9 from the product.
28
-
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:
29
28
30
29
```text
31
30
8569 6195 0383 3437
32
31
```
33
32
34
-
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.
Copy file name to clipboardExpand all lines: exercises/practice/luhn/.docs/introduction.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,10 @@
2
2
3
3
At the Global Verification Authority, you've just been entrusted with a critical assignment.
4
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 ensure these numbers are valid and error-free.
5
+
The Luhn algorithm is a simple checksum formula used to help identify mistyped numbers.
6
6
7
7
A batch of identifiers has just arrived on your desk.
8
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 errors or fraud, such as incorrect transactions or unauthorized access.
9
+
If any fail, they'll be flagged as invalid, preventing mistakes such as incorrect transactions or failed account verifications.
10
10
11
11
Can you ensure this is done right? The integrity of many services depends on you.
0 commit comments