Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Luhn #23

Open
wants to merge 1 commit into
base: trinary
Choose a base branch
from
Open

Luhn #23

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions luhn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Luhn
def initialize(num)
@number = num
end
def addends
temp_loop = @number
@result_chunks = []
count = 1
while temp_loop > 0
digit_chunk = temp_loop % 10
if count % 2 == 0
check_9 = digit_chunk * 2
if check_9 > 9
@result_chunks << (check_9 - 9)
else
@result_chunks << check_9
end
else
@result_chunks << digit_chunk
end
temp_loop = temp_loop / 10
count += 1
end
@result_chunks.reverse!
end
def checksum
addends
@sum_array = @result_chunks.inject(&:+)
@sum_array
end
def valid?
checksum
if @sum_array % 10 == 0
true
else
false
end
end
def self.create(number)
arr = Luhn.new(number).addends
new_sum = 0
number = number * 10 + (10 - Luhn.new(number * 10).checksum) % 10
number
end
end