Skip to content

Instantly share code, notes, and snippets.

@tomazas
Last active April 7, 2017 08:49
Show Gist options
  • Save tomazas/ade547d13ded07f7cb472b3896ae871e to your computer and use it in GitHub Desktop.
Save tomazas/ade547d13ded07f7cb472b3896ae871e to your computer and use it in GitHub Desktop.
Luhn python implementation
# Luhn python implementation: http://www.ee.unb.ca/tervo/ee4253/luhn.shtml
def luhn(input):
sum = 0
for i,c in enumerate(input):
num = (2-(i % 2)) * int(c)
sum += int(num/10) + (num % 10)
#print sum
return ((sum % 10) == 0)
# test
if __name__ == "__main__":
print "ok:",luhn("4563960122001999")
print "bad:",luhn("4563960122001998")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment