Created
December 2, 2016 05:01
-
-
Save Ryanspink1/e2923fa61b0a4ebd4a4930f11188ba3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Ryan Spink ### | |
###Asking for CC number: | |
print "What is your 16 digit credit card number? >" | |
####Storing the 16 digit CC number: | |
card_number = gets.chomp | |
###Storing the all the numbers in the CC as separate variables, multiplying every other by two according to the Luhn: | |
a = card_number[-1].to_i | |
b = card_number[-2].to_i*2 | |
c = card_number[-3].to_i | |
d = card_number[-4].to_i*2 | |
e = card_number[-5].to_i | |
f = card_number[-6].to_i*2 | |
g = card_number[-7].to_i | |
h = card_number[-8].to_i*2 | |
i = card_number[-9].to_i | |
j = card_number[-10].to_i*2 | |
k = card_number[-11].to_i | |
l = card_number[-12].to_i*2 | |
m = card_number[-13].to_i | |
n = card_number[-14].to_i*2 | |
o = card_number[-15].to_i | |
p = card_number[-16].to_i*2 | |
###If the odd [doubled] numbers are over 10, add their tens and ones place together: | |
if b.to_i >= 10 | |
b = (b.to_s[-1].to_i) + (b.to_s[-2].to_i) | |
end | |
if d.to_i >= 10 | |
d = (d.to_s[-1].to_i) + (d.to_s[-2].to_i) | |
end | |
if f.to_i >= 10 | |
f = (f.to_s[-1].to_i) + (f.to_s[-2].to_i) | |
end | |
if h.to_i >= 10 | |
h = (h.to_s[-1].to_i) + (h.to_s[-2].to_i) | |
end | |
if j.to_i >= 10 | |
j = (j.to_s[-1].to_i) + (j.to_s[-2].to_i) | |
end | |
if l.to_i >= 10 | |
l = (l.to_s[-1].to_i) + (l.to_s[-2].to_i) | |
end | |
if n.to_i >= 10 | |
n = (n.to_s[-1].to_i) + (n.to_s[-2].to_i) | |
end | |
if p.to_i >= 10 | |
p = (p.to_s[-1].to_i) + (p.to_s[-2].to_i) | |
end | |
### Add the original and adjusted variables together, as required by the Luhn: | |
firstcheck = a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p | |
###my check to see the final number output: | |
#puts firstcheck.to_i | |
###Output. If the ones place of the summed variables is 0, output valid. Else, Invalid. | |
if (firstcheck.to_s[-1].to_i) === 0 | |
puts "Valid" | |
else | |
puts "Invalid" | |
end | |
### It does work for the Amex numbers provided. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment