Problem of the Day
A new programming or logic puzzle every Mon-Fri

Credit Card Validator

Our new business venture is getting ready to launch and we need a way to take credit cards. Before sending the customers credit card number off to the payment processor we want to make sure the card number is valid. Today's objective is to create a program to check if a credit card number is valid. Feel free to use an int array if needed.

*Credit cards are validated using the Luhn Algorithm with an expected check digit of modulo 10.

Permalink: http://problemotd.com/problem/credit-card-validator/

Comments:

  • Anonymous - 10 years, 5 months ago

    Late and not optimal but still haskell :D

    main :: Int -> Int -> Int ->Int -> Bool
    main x y z a = checkNr == testNr
        where
            i = intToList x
            k = intToList y
            l = intToList z
            m = intToList a
            list = i ++ k ++ l ++ m
            luhnList = [f x | (x,f) <- zip revListLuhn $ cycle [(*2),id]]
            revList = reverse list
            revListLuhn = drop 1 revList
            checkNr = head revList 
            testNr = (-) 10 $ mod luhnS 10
            luhnS = luhnSum luhnList 0
    
    intToList :: Int -> [Int]
    intToList 0 = []
    intToList x = (intToList $ quot x 10) ++ [(mod x 10)] 
    
    luhnSum :: [Int] -> Int -> Int
    luhnSum [] x = x
    luhnSum (x:xs) i = luhnSum xs i+y
        where
            y = if x > 9 then mod x 10 + quot x 10 else x
    

    i split the input into 4 numbers (a wrapper is pretty simple so i left it out here)

    reply permalink

  • Anonymous - 10 years, 5 months ago

    #python 2.7
    def is_valid_card(a):
        b = []
        for letter in a:
            b.append(int(letter))
    
        #double every other except last digit
        i = 0
    
        while i < (len(b)-1):
            if i % 2 == 0:
                if b[i] * 2 < 10:
                    b[i] = b[i] * 2
                else:
                    b[i] = (b[i] * 2) % 10 + 1
            i = i + 1
    
        #sum the digits, mod 10
        i = 0
        sum = 0
        while i < len(b):
            sum = sum + b[i]
            i = i + 1
    
        if sum%10 == 0:
            return True
        else:
            return False
    

    reply permalink

  • bumbleguppy - 10 years, 5 months ago

    The wiki page links to a much better javascript solution, but this was good exercise for me anyway.

    function luhnAlgorithm(num){
        var sum = 0;
        var adder = 0;
        var flag = true;
        var check = num % 10;
        num = Math.floor(num / 10);
        while(num){
            if(flag){
                adder = 2 * (num % 10);
                if(adder > 9){
                    while(adder){
                        sum += adder % 10;
                        adder = Math.floor(adder / 10);
                    }
                }else{
                    sum += adder;
                }
            }else{
                sum += num % 10;
            }
    
            flag = !flag; //this is used to check every other digit
            num = Math.floor(num / 10);
        }
        return (sum * 9) % 10 === check;
    }
    
    console.log(luhnAlgorithm2(79927398714));
    

    reply permalink

  • David - 10 years, 5 months ago

    Have some C. It's good for you. ''' #include <stdio.h> #include <math.h>

    
    bool verifyCard(unsigned short cardNumberTop, unsigned short cardNumberUpper, unsigned short cardNumberMiddle, unsigned short cardNumberLower) {
      unsigned short luhnSum = 0;
      char temp = 0;
      unsigned short residue = 0;
      unsigned short cardNumber[] = {cardNumberTop, cardNumberUpper, cardNumberMiddle, cardNumberLower}; // This could probably have just been the function argument.
      char index; char group;
      if(0 == cardNumberTop | 0 == cardNumberUpper | 0 == cardNumberMiddle | 0 == cardNumberLower) {
        return 0; //Optional: I assume "0000" is frowned on for selecting a card number. 
      }
      for(group = 0; group < 4; group++) {
        residue = cardNumber[group];
        for(index = 0; index < 4; index++){
          temp = (1 << (1-index%2)) * (residue / (int) pow(10,index) % 10); luhnSum += (temp % 10) + (temp / 10); //residue /= 10;
        }
      }
      return 0 != luhnSum%10;
    }
    
    int main(void) {
      ...[running and stuff]... 
    } 
    

    I also decided to break up the number into 4 parts, since the number on the card if often separated that way.

    reply permalink

Content curated by @MaxBurstein