#include #include int luhn(const char* cc) { const int m[] = {0,2,4,6,8,1,3,5,7,9}; // mapping for rule 3 int i, odd = 1, sum = 0; for (i = strlen(cc); i--; odd = !odd) { int digit = cc[i] - '0'; sum += odd ? digit : m[digit]; } return sum % 10 == 0; } int main() { const char* cc[] = { "49927398716", "49927398717", "1234567812345678", "1234567812345670", 0 }; int i; for (i = 0; cc[i]; i++) printf("%16s\t%s\n", cc[i], luhn(cc[i]) ? "ok" : "not ok"); return 0; } public class Luhn { public static void main(String[] args) { System.out.println(luhnTest("49927398716")); System.out.println(luhnTest("49927398717")); System.out.println(luhnTest("1234567812345678")); System.out.println(luhnTest("1234567812345670")); } public static boolean luhnTest(String number){ int s1 = 0, s2 = 0; String reverse = new StringBuffer(number).reverse().toString(); for(int i = 0 ;i < reverse.length();i++){ int digit = Character.digit(reverse.charAt(i), 10); if(i % 2 == 0){//this is for odd digits, they are 1-indexed in the algorithm s1 += digit; }else{//add 2 * digit for 0-4, add 2 * digit - 9 for 5-9 s2 += 2 * digit; if(digit >= 5){ s2 -= 9; } } } return (s1 + s2) % 10 == 0; } } >>> def luhn(n): r = [int(ch) for ch in str(n)][::-1] return (sum(r[0::2]) + sum(sum(divmod(d*2,10)) for d in r[1::2])) % 10 == 0 >>> for n in (49927398716, 49927398717, 1234567812345678, 1234567812345670): print(n, luhn(n)) #include using namespace std; int toInt(const char c) { return c-'0'; } int confirm( const char *id) { bool is_odd_dgt = true; int s = 0; const char *cp; for(cp=id; *cp; cp++); while(cp > id) { --cp; int k = toInt(*cp); if (is_odd_dgt) { s += k; } else { s += (k!=9)? (2*k)%9 : 9; } is_odd_dgt = !is_odd_dgt; } return 0 == s%10; } int main( ) { const char * t_cases[] = { "49927398716", "49927398717", "1234567812345678", "1234567812345670", NULL, }; for ( const char **cp = t_cases; *cp; cp++) { cout << *cp << ": " << confirm(*cp) << endl; } return 0; } public static class Luhn { public static bool LuhnCheck(this string cardNumber) { return LuhnCheck(cardNumber.Select(c => c - '0').ToArray()); } private static bool LuhnCheck(this int[] digits) { return GetCheckValue(digits) == 0; } private static int GetCheckValue(int[] digits) { return digits.Select((d, i) => i % 2 == digits.Length % 2 ? ((2 * d) % 10) + d / 5 : d).Sum() % 10; } } public static class TestProgram { public static void Main() { long[] testNumbers = {49927398716, 49927398717, 1234567812345678, 1234567812345670}; foreach (var testNumber in testNumbers) Console.WriteLine("{0} is {1}valid", testNumber, testNumber.ToString().LuhnCheck() ? "" : "not "); } } def luhn(code) csum = 0 code.digits.each_slice(2) do |odd, even| double = even.to_i * 2 double -= 9 if double > 9 csum += double + odd end csum % 10 == 0 end [49927398716, 49927398717, 1234567812345678, 1234567812345670].each do |n| p [n, luhn(n)] end func lunhCheck(number : String) -> Bool { let reversedInts = number.characters.reversed().map { Int(String($0)) } return reversedInts.enumerated().reduce(0, {(sum, val) in let odd = val.offset % 2 == 1 return sum + (odd ? (val.element! == 9 ? 9 : (val.element! * 2) % 9) : val.element!) }) % 10 == 0 } lunhCheck("49927398716") // true lunhCheck("49927398717") // false luhnTest <- function(cc){ # Reverse the digits, convert to numeric vector cc2 <- as.numeric(unlist(strsplit(as.character(cc), "")))[nchar(cc):1] s1 <- 0 s2 <- 0 for (index in 1:length(cc2)){ if (index %% 2 == 1){ s1 <- sum(s1, cc2[index]) } else if (cc2[index] >= 5){ s2 <- sum(s2, (cc2[index]*2 - 9)) } else { s2 <- sum(s2, (cc2[index]*2)) } } return ((s1 + s2) %% 10 == 0) } sub luhn_test { my @rev = reverse split //,$_[0]; my ($sum1,$sum2,$i) = (0,0,0); for(my $i=0;$i<@rev;$i+=2) { $sum1 += $rev[$i]; last if $i == $#rev; $sum2 += 2*$rev[$i+1]%10 + int(2*$rev[$i+1]/10); } return ($sum1+$sum2) % 10 == 0; } print luhn_test('49927398716'); print luhn_test('49927398717'); print luhn_test('1234567812345678'); print luhn_test('1234567812345670'); - (NSArray *) toCharArray { NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[self length]]; for (int i=0; i < [self length]; i++) { NSString *ichar = [NSString stringWithFormat:@"%C", [self characterAtIndex:i]]; [characters addObject:ichar]; } return characters; } + (BOOL) luhnCheck:(NSString *)stringToTest { NSArray *stringAsChars = [stringToTest toCharArray]; BOOL isOdd = YES; int oddSum = 0; int evenSum = 0; for (int i = [stringToTest length] - 1; i >= 0; i--) { int digit = [(NSString *)stringAsChars[i] intValue]; if (isOdd) oddSum += digit; else evenSum += digit/5 + (2*digit) % 10; isOdd = !isOdd; } return ((oddSum + evenSum) % 10 == 0); } BOOL test0 = [self luhnCheck:@"49927398716"]; //Result = YES BOOL test1 = [self luhnCheck:@"49927398717"]; //Result = NO BOOL test2 = [self luhnCheck:@"1234567812345678"]; //Result = NO BOOL test3 = [self luhnCheck:@"1234567812345670"]; //Result = YES