Check out example codes for "first k digits of n*n". It will help you in understanding the concepts better.

Code Example 1

factor = n mod 10^k
result = 1
while (n != 0) 
    if (n is odd) then result = (result * factor) mod 10^k
    factor = (factor * factor) mod 10^k
    n >>= 1

Learn ReactJs, React Native from akashmittal.com