Problem Statement
You know that the Luhn formula applies for all the acceptable card numbers.
The Luhn formula works as follows.
First, separate the individual digits of the credit card number. For example:
21378 becomes
2 1 3 7 8
If there is an even number of digits, multiply each digit in an odd position by 2. Otherwise, multiply each digit in an even position by 2. Positions are 1-indexed, so the first digit is at position 1. The example number above contains an odd number of digits, so we multiply each digit in an even position by 2:
2 1 3 7 8 becomes
2 2 3 14 8
Note that the even positions refer to the original number so they don't change even when a 2-digit number appears.
Finally, take the sum of all the digits (for 2-digit numbers insert both the digits separately into the sum):
2+2+3+1+4+8 = 20
If the sum is a multiple of 10, the number is valid. Otherwise, it is invalid.
Given a
Definition
- Class:
- VerifyCreditCard
- Method:
- checkDigits
- Parameters:
- String
- Returns:
- String
- Method signature:
- String checkDigits(String cardNumber)
- (be sure your method is public)
Constraints
- cardNumber will contain between 1 and 50 characters, inclusive.
- Each character in cardNumber will be a digit ('0'-'9').
Examples
"21378"
Returns: "VALID"
This number has 5 digits, which is an odd number, so we multiply the digits in even positions by 2 to get: 2 2 3 14 8 The sum of the digits is 20, which is a multiple of 10, meaning it's a valid number.
"31378"
Returns: "INVALID"
When we apply the Luhn formula here, the sum of the digits is 21, so the number is invalid.
"11111101"
Returns: "VALID"
We multiply the digits in odd positions by 2 to get: 2 1 2 1 2 1 0 1 The sum of the digits is 10, so it's a valid card.
"50005"
Returns: "VALID"
All the digits in even positions are 0 so multiplying by 2 doesn't change the number. The sum of the digits is 10, so it's a valid card.
"542987223412"
Returns: "INVALID"
"91"
Returns: "VALID"
"59568"
Returns: "VALID"
"99476855"
Returns: "VALID"
"72570633544"
Returns: "VALID"
"23321694490569"
Returns: "VALID"
"88106566792269381"
Returns: "VALID"
"70227645677166800029"
Returns: "VALID"
"21047627293442904261501"
Returns: "VALID"
"79127827840531882116825684"
Returns: "VALID"
"28961242341484572254466523926"
Returns: "VALID"
"25364925833897951720819099911835"
Returns: "VALID"
"61032330626219552613054515880636534"
Returns: "VALID"
"19290330449571267952817267399620211473"
Returns: "VALID"
"74922972607332151132338752676476341408348"
Returns: "VALID"
"81867094695525064006190464380740237559855193"
Returns: "VALID"
"42669548126204487976132951004015731353279892806"
Returns: "VALID"
"27017016750677705086780154746898236617814106309520"
Returns: "VALID"
"49"
Returns: "INVALID"
"61621"
Returns: "INVALID"
"95178989"
Returns: "INVALID"
"29093335373"
Returns: "INVALID"
"32954619575167"
Returns: "INVALID"
"22539207302620951"
Returns: "INVALID"
"64077257787006697336"
Returns: "INVALID"
"26227174957722514961366"
Returns: "INVALID"
"12832046134938915145774054"
Returns: "INVALID"
"58397339162308565128348011892"
Returns: "INVALID"
"82454218665928174671917666630233"
Returns: "INVALID"
"26428845325300615167107523257347460"
Returns: "INVALID"
"50374011226424691838735023319005512983"
Returns: "INVALID"
"97579853894121818386199015645028241263121"
Returns: "INVALID"
"14654562747376652905169683538132134738382198"
Returns: "INVALID"
"21203836254012751186313077845267326827113534829"
Returns: "INVALID"
"82632318411198690768949665112363891016569914570654"
Returns: "INVALID"
"0"
Returns: "VALID"
"9"
Returns: "INVALID"
"1"
Returns: "INVALID"
"123"
Returns: "INVALID"
"133"
Returns: "VALID"
"1234"
Returns: "INVALID"
"4523"
Returns: "VALID"
"258"
Returns: "INVALID"
"565689"
Returns: "VALID"
"9999999999"
Returns: "VALID"
"5555"
Returns: "INVALID"
"101010101000000000000000000000000000"
Returns: "VALID"
"12345678912345678912345678912345678912345678987654"
Returns: "INVALID"
"9999999999999999999999"
Returns: "INVALID"
"414"
Returns: "VALID"
"141"
Returns: "VALID"
"99999999999999999999999999999999999999999999999999"
Returns: "VALID"
"0505"
Returns: "VALID"
"05"
Returns: "INVALID"