|
|
|
|
|
by 1337Coder
4528 days ago
|
|
If anyone is curious about the luhn algorithm, here is one I made in C# from the example in the question: static bool IsValidLuhn(string numbers)
{
if (numbers == null)
throw new ArgumentNullException("number", "number must have a value."); var allNumbers = numbers
.Where((c) => c >= '0' && c <= '9')
.Reverse()
.Select((c, i) => (i % 2 == 1) ? ((Convert.ToInt32(c) - 48) * 2).ToString() : c.ToString()); return allNumbers.Count() > 0 ? allNumbers.Aggregate((x, y) => x + y).Sum((c) => Convert.ToInt32(c) - 48) % 10 == 0 : false;
} Edit: Can sum one link me to HackerNews markdown? |
|