Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Last active August 29, 2015 14:13
Show Gist options
  • Save natecook1000/1eb756d6b10297006137 to your computer and use it in GitHub Desktop.
Save natecook1000/1eb756d6b10297006137 to your computer and use it in GitHub Desktop.
Luhn check modified with enumerate
func lunhCheck(number : String) -> Bool
{
return reduce(enumerate(reverse(number).map { String($0).toInt()! }), 0) {
let odd = $1.0 % 2 == 1
return $0 + (odd ? ($1.1 == 9 ? 9 : ($1.1 * 2) % 9) : $1.1)
} % 10 == 0
}
lunhCheck("49927398716")
lunhCheck("49927398717")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment