You could break the number into chunks of 10 digits (from the right) and do the modular arithmetic on the chunks, combining the result at the end:
1551690021432628813 = 155169002 * 10**10 + 1432628813
Hence
1551690021432628813 % 64 = (155169002 % 64 * (10**10) % 64 + 1432628813 % 64) % 64
(Which equals 13
).
You could write a recursive function that implements this idea. The following is in Python (which I am more fluent in) but should be easily translated into JavaScript:
def remainder(s,m):
#computes int(s) % m, while just using small numbers
#s is a string and m is an integer
n = len(s)
if n <= 10:
return int(s) % m
else:
first = s[:n-10] #first n-10 digits in s
second = s[-10:] #last 10 digits
return (remainder(first,m) * ((10**10) % m) + int(second) % m) % m
For the special case that the modulus is 64
, there is an exceptionally easy approach: 64 divides 10**6
so, absolutely always
n % 64 == (last 6 digits of n) % 64
For example,
1551690021432628813 % 64 = 628813 % 64 = 13
Similar remarks hold whenever the modulus is a power of 2.