DEV Community

Discussion on: Daily Challenge #94 - Last Digit

Collapse
 
giangdlinh profile image
Giang Vincent

can we have some explain please ?

Collapse
 
erezwanderman profile image
erezwanderman • Edited

Yes.
Let's look at a spreadsheet of xy % 10.
x^y % 10

For x, the values repeat every 10, that is, xy % 10 == (x+10)y % 10.
For y, the values repeat every 4, that is, xy % 10 == xy+4 % 10, except for the case y=0.
Why is it so? I don't know, maybe someone else can explain/prove the math. All I know is that I can use it to make the power calculations much smaller. If I need the last digit of 345435456, I calculate instead the last digit of (345435%10)(456-1%4+1)=53=125, which is 5.

So that's what I'm doing in the function, extracting the last two numbers out of the array, calculating the right-most digit of their power and pushing it back. I also handle the case of empty list as per the requirements, not sure why they wanted it to be 1.