Submit | All submissions | Best solutions | Back to list |
DIXIE004 - Luhn Algorithm |
The input consists of a bunch of lines of sample credit card numbers, each followed by a newline. Your task is to write a program that validates each credit card number according to the Luhn algorithm, which will be explained below. You should output the same number followed by a comma, and the string "TRUE" if the number is valid or "FALSE" if the number is not valid.
The formula for validating a credit card, the Luhn algorithm, can be implemented as follows:
- Sum the digits in odd-numbered positions (counting from the right-most side of the number.)
- Double each digit in an even-numbered position (again, counting from the right-most side of the number), sum the digits of the resulting values (note: not the values themselves.)
- Add the sums from steps one and two.
- If that total is evenly divisible by 10 (no remainder) the card number is considered valid.
Note that valid cards must also have a valid prefix taken from this list: [51, 52, 53, 54, 55, 4].
We are also assuming that all cards numbers in the input file are a length of 16.
Example 1
Given an example (valid) card number of 4652360088404638.
- Odd digits: 8 + 6 + 0 + 8 + 0 + 6 + 2 + 6 = 36
- Even digits 3 4 4 8 0 3 5 4
Doubled 6 8 8 16 0 6 10 8
Sum of digits 6 + 8 + 8 + 1 + 6 + 0 + 6 + 1 + 0 + 8 = 44 - Total of sums from steps 1 and 2: 36 + 44 = 80
- 80 % 10 = 0
The remainder at the end was zero, so the number is considered valid.
Example 2
Given an example (invalid) card number of 5370182444652350.
- Odd digits: 0 + 3 + 5 + 4 + 4 + 8 + 0 + 3 = 27
- Even digits 5 2 6 4 2 1 7 5
Doubled 10 4 12 8 4 2 14 10
Sum of digits 1 + 0 + 4 + 1 + 2 + 8 + 4 + 2 + 1 + 4 + 1 + 0 = 28 - Total of sums from steps 1 and 2: 27 + 28 = 55
- 55 % 10 = 5
The remainder at the end was 5, so the number is not considered valid.
Note: The ¶ symbol in the examples below represents a new-line character.
Sample Input
4652360088404638¶ 5370182444652350¶
Sample Output
4652360088404638,TRUE¶ 5370182444652350,FALSE¶
Added by: | Dixie State University |
Date: | 2013-03-30 |
Time limit: | 1.428s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | Problem from the DSU 2013 ACM Contest |
hide comments
2013-03-30 18:12:06 Arvind
Please check my submission id: 9004778 Getting WA. I think the soln is correct. please check!! :) EDIT: got it.had by mistake printed a test cout statement :D Last edit: 2013-03-30 18:08:11 |
|
2013-03-30 18:12:06 Kevin Sebastian
getting WA continously..dont knw if its bcoz of output format @author edit(DSU) - I am getting No Output to stdout. Last edit: 2013-04-08 17:42:36 |
|
2013-03-30 18:12:06 Kevin Sebastian
please check my submission id 9003928 edit(DSU) - You had an extra '\n' char but I noticed you fixed it in more recent submissions. Last edit: 2013-03-30 18:00:21 |
|
2013-03-30 18:12:06 joud zouzou
1st one to solve this problem :) yayyyy!! |
|
2013-03-30 18:12:06 brahim
In the second example, 0 + 3 + 5 + 4 + 4 + 8 + 0 + 3 = 25 is false. the result is 27. edit(francky) : I've edit the body to correct that point. edit(DSU): Well that is embarrassing... Thanks for the catch! Last edit: 2013-03-30 16:53:42 |
|
2013-03-30 18:12:06 Francky
The problem is obvious, can you tell me the problem with my submission? There's perhaps an input formating problem. edit:OK, there was no space between ',' and 'TRUE'/'FALSE'. Please consider Pyramid cluster, and only 1s will be enough. People may ask to move it to tutorial... Last edit: 2013-03-30 09:32:34 |