ECOO '13 R1 P2 - The Luhn Algorithm

View as PDF

Submit solution

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

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

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

9 \times 2 = 18 \rightarrow 1 + 8 = 9

2 \times 2 = 4

4 + 9 = 13

Step 2

13 + 4 + 3 + 5 = 25

Step 3

25 is not divisible by 10.

Not valid.

Example 2: Validate 35436

Step 1

3 \times 2 = 6

5 \times 2 = 10 \rightarrow 1 + 0 = 1

1 + 6 = 7

Step 2

7 + 3 + 4 + 6 = 20

Step 3

20 is divisible by 10.

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 5 test cases. Each test case consists of a batch of 5 base numbers (1 to 100 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 5-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


Comments


  • 1
    Andy_Hou  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


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

      See wleung_bvg's answer to InputMismatchException below.


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

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


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

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


    • 2
      wleung_bvg  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.


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

    cool stuff!


  • 1
    Oppenheimer  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.


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

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