Skip to content

Instantly share code, notes, and snippets.

@akshaymohite
Last active October 10, 2019 22:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshaymohite/c97f15a4de57175a9cdf2ab313cfdb49 to your computer and use it in GitHub Desktop.
Save akshaymohite/c97f15a4de57175a9cdf2ab313cfdb49 to your computer and use it in GitHub Desktop.
Luhn's Algorithm to validate card numbers
number = "314143525252"
sum = 0
number.reverse.split("").each_slice(2) do |x,y|
sum += x.to_i + (2*y.to_i).divmod(10).sum
end
p sum%10 == 0 ? 'valid card number' : 'invalid card number'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment