Skip to content

Instantly share code, notes, and snippets.

@tt
Created February 5, 2010 21:49
Show Gist options
  • Save tt/296302 to your computer and use it in GitHub Desktop.
Save tt/296302 to your computer and use it in GitHub Desktop.
public long CalculateChecksum(long value)
{
long sum = 0;
for (int i = 0; value != 0; i++)
{
for (var j = (value % 10) * (2 - i % 2); j != 0; j /= 10)
{
sum += j % 10;
}
value /= 10;
}
return (10 - sum % 10) % 10;
}
public long CalculateChecksum(long value)
{
var sum = value.ToString()
.Reverse()
.Select((c, i) => int.Parse(c.ToString()) * (2 - i % 2))
.Select(i => i.ToString().Sum(c => c - '0'))
.Sum();
return (10 - sum % 10) % 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment