Validating Credit Card Numbers

Submitted By:
irkenInvader

Difficulty:
Medium

Tags:
strings, enumerables

Instructions:
Credit card numbers can be validated with a process called the Luhn algorithm. Simply stated, the Luhn algorithm works like this:

  1. Starting with the next to last digit and continuing with every other digit going back to the beginning of the card, double the digit.
  2. Sum all doubled and untouched digits in the number.
  3. If that total is a multiple of 10, the number is valid.
For example, given the card number 4408 0412 3456 7893:
Orig  :  4 4 0 8 0 4 1 2 3 4   5 6   7 8   9 3
Step 1:  8 4 0 8 0 4 2 2 6 4  10 6  14 8  18 3
Step 2:  8+4+0+8+0+4+2+2+6+4+1+0+6+1+4+8+1+8+3 = 70
Step 3:  70 % 10 == 0

This card is valid!

Code:
def valid_credit_card?(num)
  ___
end


assert_equal valid_credit_card?("1234567890123456"), false
assert_equal valid_credit_card?("4408041234567893"), true
assert_equal valid_credit_card?("440804l234567893"), false
assert_equal valid_credit_card?("38520000023237"), true # diner's club
assert_equal valid_credit_card?("4222222222222"), true # visa cards have either 13 or 16 digits

Your Solution

Ruby 1.9.3

Back to Problems