Search code examples
c++digit

Finding the last digit of a very large 2^n number


As the title sais, i want to find the last digit of an 2^n number.

I know that it's about repetition of even numbers, but i don't know how to make it happen.

This is what I did:

return (2*ascii%10)%10 - 2;

But it's not the correct method

For example:

I got 2^97, find the last digit of this very large number. Thank you!


Solution

  • Powers of 2:

    2, 4, 8, 16, 32, 64, 128, 256...
    

    Last digits:

    2, 4, 8, 6, 2, 4, 8, 6...
    

    Last digit of 2^i is:

    int arr[4] = {2, 4, 8, 6};
    return arr[(i-1)%4];