1

I am working on a problem in Ruby. The function called last_digit takes in two integers. The first integer is then raised to the power of the second integer. The function must return the last digit of that integer after this calculation has been completed.For example, if the first integer was 2 and the second 4, then the function would do 2 ** 4, which equals 16 and then return the last digit of that number, which is 6.

I have written a ruby function to do this but once the numbers get too large I believe the result defaults to infinity and my function returns the wrong answer of 0. My ide also brings up the following warning:

warning: in a**b, b may be too big 469Infinity

My code followed by my test cases can be seen below:

def last_digit(n1, n2)
  num = n1 ** n2
  if num == 0
    return print 1
  end
  return print num.to_s[-1].to_i
end

last_digit(4, 1)                # returns 4
last_digit(4, 2)                # returns 6
last_digit(9, 7)                # returns 9
last_digit(10, 10 ** 10)        # return 0
last_digit(2 ** 200, 2 ** 300)  # should return 6 but returns 0

Note it is the last two calls of last_digit that cause this warning / wrong return. Also note that if the result of the numbers to the power of each other is 0 then the function should return 1 so ignore lines

if num == 0
   return print 1
end
  • 1
    `10 ** (10 ** 10)` would have `(10 ** 10) + 1` (i.e., ten billion and one) decimal digits, requiring over 33 billion bits, or almost 4 gigabytes of memory just to store. The last example is clearly much worse. – Karl Knechtel May 29 '22 at 16:27
  • Does this answer your question? [Large Exponents in Ruby?](https://stackoverflow.com/questions/1758669/large-exponents-in-ruby) (Hint: getting the last digit of a number is equivalent to working modulo 10.) – Karl Knechtel May 29 '22 at 16:27
  • @steenslag typo, corrected now. I meant 2 ** 4. Reading into modular exponentiation as suggested by Karl above's comments – Machiavellie May 29 '22 at 16:58
  • `2**300` is an integer with 91 digits. You use **this** huge number as an exponent, and the base for the exponentiation (2**200) still is a 61 digit number!!!! What miracles do you expect from Ruby's integer arithmetic? – user1934428 May 30 '22 at 07:12
  • This has nothing to do with Ruby. It is just common sense: you are computing `a ** b`, where *each of `a` and `b`* are bigger than the number of particles in the known universe. So, you are multiplying a number that is larger than the entire universe by itself more times than there are particles in the known universe. The result of your computation requires 50930000000000000000000000000000000000000000000000000000000000000000000000000 petabytes of RAM. Do you have that amount of RAM installed in your PC? What do you expect to happen there? – Jörg W Mittag May 30 '22 at 08:45
  • 50930000000000000000000000000000000000000000000000000000000000000000000000000 petabytes of RAM ought to be enough for everybody. – steenslag May 30 '22 at 15:09

1 Answers1

8

What you are looking for is modular exponentiation. It is supported by ruby, but sort of hidden in the docs; just an extra option of Integer's pow.

def last_digit(n1, n2)
  n1.pow(n2, 10)
end
steenslag
  • 79,051
  • 16
  • 138
  • 171