Reputation: 878
I'm wondering if there is a trick with number theory to compute this remainder without need to implement a BigInt division algorithm.
Upvotes: 1
Views: 1093
Reputation: 7597
Its simple, just check these three things:
Divisibility by 1500
00
)0
or 5
)And if you want to know the remainder, its again simple:
Check for divisible by 5 and get remainder
500
, it will be from 0
to 499
.Check for divisible by 3 and get remainder
iterate over all digits, sum them, and get remainder from that after division by 3
, it will be from 0
to 2
.
and depending from this remainder increase the remainder from 1st step by this remainder multiplied by 500
.
Example 1
1234567890 % 1500 = 390
7890 % 500 = 390
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 = 45
and 45 % 3 = 0
, so nothing has to be added to 390
and the result is then 390
.Example 2
12345678901 % 1500 = 901
8901 % 500 = 401
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + 1 = 46
and 46 % 3 = 1
, so we have to add 1 * 500
to the result from 1st step, so 401 + 1 * 500 = 901
.Example 3
1357913579 % 1500 = 1079
3579 % 500 = 79
1 + 3 + 5 + 7 + 9 + 1 + 3 + 5 + 7 + 9 = 50
and 50 % 3 = 2
, so we have to add 2 * 500
to the result from 1st step, so 79 + 2 * 500 = 1079
.Hope this helps you.
Upvotes: 1
Reputation: 878
Haha, it's easy!
I can iterate over all digits, adding each parcel... Using the properties:
1) (a+b) mod c = (a mod c + b mod c) mod c
2) (a*b) mod c = (a mod c * b mod c) mod c
The power of ten can be increased mod 1500 each step.
Upvotes: 1