using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
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 class Program
{
public static void Main(string[] args)
{
long[] testNumbers = {800112794657, 6011000990139424};
foreach (var testNumber in testNumbers)
Console.WriteLine("{0} is {1}valid", testNumber, testNumber.ToString().LuhnCheck() ? "" : "not ");
}
}
}
|