Last active
August 31, 2015 03:28
-
-
Save salizzar/7f5187afb28e70fa1f0d to your computer and use it in GitHub Desktop.
A little ruby script to get sum of all digits reduced to one, given an array of random numbers between an interval.
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
random = Random.new | |
lower = 1000000000 | |
upper = lower * 2 - 1 | |
numbers = 10.times.collect { random.rand(lower..upper) } | |
summarizer = lambda do |number| | |
array = number.to_s.split("").collect(&:to_i) | |
array.inject { |sum, i| sum += i } | |
end | |
numbers.each do |number| | |
sum = total = summarizer.call(number) | |
stack = [] | |
stack << number | |
stack << total | |
if sum < 10 | |
stack << sum | |
else | |
new_sum = sum | |
loop do | |
new_sum = summarizer.call(new_sum) | |
stack << new_sum | |
break if new_sum < 10 | |
end | |
end | |
puts stack.join(" - ") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muito bom! :D