Get the modulus of a Number N concatenated N times.

992 Views Asked by At

I don't know if this is the right forum.

Is there a Mathematical approach to get the Modulus of a Number N which is concatenated N times?

I had the following scenario today on an Interview for a Developer Job, this was one of the questions, and only one I fail to answer.

Concatenate the number N N times and then calculate his Modulus by 2017.

For example: for N=5 the number will be 55555 and the result will be Mod(55555,2017) = 1096 for N=10 the number will be 10101010101010101010 and the result Mod(10101010101010101010,2017) = 1197

Now the Number I had to calculate was 58184241583791680. The only hint I got was that the result of 58184241583791680 concatenated 58184241583791680 times modulus 2017 is a 4 digit number.

My question is: Can we reduce somehow this number or the only solution is to concatenate it 58184241583791680 times?.

1

There are 1 best solutions below

7
On BEST ANSWER

The concatenation of $K$ copies of $N$ is equal to:

$\sum\limits_{i=0}^{K-1}(10^{L\times i}N)$ where $L$ is the number of digits in $N$. This is because every copy can be thought of as adding $N$ with some zeros to the right.

We can rewrite this as: $N\times \sum\limits_{i=0}^{k-1}10^{L\times i}=N\times(10^{L\times K}-1)\times (10^L-1)^{-1}\equiv N\times(10^{L\times K}-1)\times(10^L-1)^{MOD-2}$ (given that the $MOD$ is prime, which is the case for $2017$)

complexity: $\mathcal O(\log(L)+\log(K)+\log(MOD))$