The user will input 2 integers, x and y, using standard input. You need to calculate the remainder of x modulo y (x % y).
Now, here is the problem:
0 <= x <= 10^100000, (this is "less than or equal" not an arrow). 0 < y <= 100000
We are looking for optimal solution (Time Complexity).
I didn't find the answer on Stackoverflow, so after finding the solution I thought that I should pose this question here for future reference and to see if there are better ways.
The solution that is better than BigInteger is the following:
1- Read the x as String and y as an integer.
2- Cut the first 9 characters from the left of x and convert them to integer i.
3- calculate i % y and append it to the beginning of x.
4- repeat from 2 until the x string is less than 7 (max length of int - max length of divider).
5- concatenate s with what is left of x and calculate the modulus.
This solution is not constrained to any integer length.
BigInteger has the divideAndRemainder(...) method, one that returns a BigInteger array, the first item the division result, and the second, the remainder (which is what mod really does in Java).
Update
Including comment by Mark Dickinson to other answer:
Implemented all 3 algorithms as described:
Testing with seeded random number for verifiable test (didn't want to hardcode a 98765 digit number):
Output:
All 3 produced same result, but there's a clear difference in performance, and Mr. M's "better solution" is the worst of them all.