DEV Community

Discussion on: Daily Challenge #250 - Last Digit of a Large Number

Collapse
 
daviducolo profile image
Davide Santangelo • Edited

ruby

def last_digit(a, b)
  (a**b).digits.first
end 

or

def last_digit(a, b)
  (a**b).to_s[-1].to_i
end 
Collapse
 
wrightdotclick profile image
TJ Wright

I think this Ruby solution underestimates the complexity required with huge numbers, although the .digits.first is clever! Using a mod 10 solution, like some of the solutions above, would greatly reduce the time/space complexity. brilliant.org/wiki/finding-the-las...