# Turning an Integer into an array of ints

My goal here is pretty simple, I want to turn a number like 12435987234 into an array of integers in reverse order. So that would look like:

[4,3,2,7,8,9,5,3,4,2,1]


I want to do this without changing the number to a string and back. Really, I don't want to convert it at all but I'm open to hearing ideas around this.

I've found one way to do in ruby via #divmod but suspect there is a more perfomant way.

# turns 1234 into
# [4,3,2,1]
def reversed_digits(val)
quot = val
results = []
while quot > 0 do
quot, remainder = quot.divmod(10)
results << remainder
end
results
end

puts reversed_digits(1234)
=> [4,3,2,1]

• I benchmarked doing it with a string, and it's about 3x faster than this method. Are you trying to do it without conversion just for the intellectual challenge? – Jonah Jan 21 '16 at 1:15
• Thanks @Jonah - I saw some similar benchmark results on my end and was hoping to tighten the above up and do a few other comparisons as well. So yes, intellectual challenge you could say. – Anthony Jan 21 '16 at 1:21
• What is the reverse of 0? – greybeard Jan 21 '16 at 1:26
• @greybeard fair question. This method today takes integers roughly the size of a credit card number (16-24) digits so the scenario smaller isn't a concern and I would likely guard against it. – Anthony Jan 21 '16 at 1:29
• If you mention data like card numbers; be aware such data may have leading zeros, so they might be stored as strings rather than numbers, and conversion to string would not be necessary at all. However, if they are numeric variables, possibly your conversion routine should take additional parameter to describe the output size (number of items to pad to with zeros)? If implemented, that would cover the zero input value case, too, with desired_output_length=1. – CiaPan Jan 21 '16 at 10:41

def reversed_digits(num, base: 10)

• why is it not? reversed_digits is the final call in the function, when it recurses – Jonah Jan 21 '16 at 18:31