## ECOO '13 R1 P2 - The Luhn Algorithm

View as PDF

Points: 5 (partial)
Time limit: 30.0s
Memory limit: 256M

Problem type

In the 1950's, Hans Peter Luhn invented a method for checking the validity of ID numbers. This method (known as the Luhn Algorithm or the Luhn Formula) is still used today for a number of different purposes, including all major credit card numbers and Social Insurance Numbers.

Here's how the Luhn Algorithm works when checking for a valid ID number:

1. Starting from the right, double every second digit, add up the digits of the result, and total up all the resulting numbers.
2. Add to this total the sum of all the remaining digits.
3. If the result is divisible by 10, the id number is valid.

#### Example 1: Validate 42395

Step 1

Step 2

Step 3

is not divisible by .

Not valid.

#### Example 2: Validate 35436

Step 1

Step 2

Step 3

is divisible by .

Valid.

#### Explanation

The last digit of every number is the "check digit" and the rest of it is the base number. So in the first example above, 4239 is the base number and 5 is the check digit. When generating card numbers or other ID numbers, you first generate the base number without the final digit, then you figure out what the check digit has to be to make the whole ID number valid.

#### Specification

The input will contain test cases. Each test case consists of a batch of base numbers ( to digits each) on one line, each separated by a single space character. Your job is to compute the check digit for each base number in the batch and then output the result as a single -digit number.

#### Sample Input

389796 4565280784 8451692334 46 465949539
97699 7392253 54011409 8073542288 303142477
334 349839 12593962 02497993 9468
53173 2901524 2493367526 39094 83530
08080532 5023002 57849 9853641952 027179

#### Sample Output

48336
36757
31920
15686
88201

Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org

• commented on Aug. 25, 2019, 8:41 p.m.

why i work out correct in eclipse but show ir in the test i check my output it is correct

• commented on Aug. 27, 2019, 11:12 a.m.

See wleung_bvg's answer to InputMismatchException below.

• commented on March 21, 2019, 1:24 p.m.

I can't figure out why I'm getting the last test case incorrect.

• commented on Sept. 27, 2018, 9:28 a.m.

java.util.InputMismatchException Can the input not be taken in as long?

• commented on Sept. 27, 2018, 9:48 a.m.

Since each number can be up to 100 digits, and a long can hold around 19 digits, you will need to find a different way to store the input.

• commented on Feb. 28, 2017, 9:44 a.m. edited

cool stuff!

• commented on Sept. 14, 2014, 5:54 p.m.

So are all these numbers presented here inputs? If so, where are the outputs? Also, if we are given, say a 6 digit number, are we supposed to find the check digit and then add it to make it into a 7 digit number? Because the last sentence says "output the result as a single 5-digit number" which doesn't make sense.

• commented on Jan. 6, 2015, 7:19 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.