Testing for Divisibility by 19
21–30 of 42 posts
Re: Testing for Divisibility by 19
#22Re: Testing for Divisibility by 19
#23can this kind of trick be implemented in a compiler for any benefit?
Other similar compiler optimizations do exist, for, for example, reducing a multiplication by a constant into a series of additions and bit-shifts. It might be possible to reduce divisibility checks for constants in binary at compile time in a similar manner, but no existing work comes to mind.
Re: Testing for Divisibility by 19
#24can this kind of trick be implemented in a compiler for any benefit?
I don't think so. It's not really more efficient than long division, with the same number of operations for each digit of the big number. Most of the improvement for humans comes from the fact that you get to deal with smaller numbers (0-9) for a section of those operations than with the bigger range 0-19, but the computer generally doesn't find it easier to multiply by one 64-bit number than another. In any case, in…
A division instruction can take a variable length of cycles to solve depending on the complexity of the division, it has terrible throughput and can be 100x slower than something like an addition, and that's on modern architectures!
Did you ever notice how a calculator can sometimes take a visible amount of time to compute something, and sometimes it was instant? Some instructions are more expensive than others!
Re: Testing for Divisibility by 19
#25Also, that earlier post introduces a graph-/regex-/dfa-based solution for solving divisibility that reminds me of [2].
Re: Testing for Divisibility by 19
#26I.e. (a * b * c) % N = (((a * b) % N) * c) % N
Re: Testing for Divisibility by 19
#27Mark's earlier post [1] which covered divisibility much more extensively mentioned a division algorithm that I've never learned: short division. Also, that earlier post introduces a graph-/regex-/dfa-based solution for solving divisibility that reminds me of [2]. [1]: https://blog.plover.com/math/divisibility-by-7.html [2]: https://codegolf.stackexchange.com/a/75326
Re: Testing for Divisibility by 19
#28> The result will be a smaller number which has the same mod-19 residue. It seems to me that the procedure works only for numbers divisible by 19. If it's not divisible, the residue may change. Try 20 for example.
Take a number say 123. Write it as 1 * 10^2 + 2 * 10 + 3.
In the first step we multiply parts of this expression by 20. This doesn't change the residue since 20 = 1 mod 19
1 * 10^2 + 2 * 10 + 3
1 * 10^2 + (2+3*2) * 10
1 * 10^2 + 8 * 10
(1+8*2) * 10^2
17 * 10^2
Now heres the part where we do change the residue. We note that 10 and 19 are coprime. So 17 * 10^2 is divisible by 19 if and only if 17 is. So replacing 17 * 10^2 with 17 might change residue, but wont change divisibility.Re: Testing for Divisibility by 19
#29https://en.wikibooks.org/wiki/Vedic_Mathematics/Sutras/Ekadh...
Re: Testing for Divisibility by 19
#30E.g. 228? 240 = 12x20, 240-12 = 228.
2337? 2360 = 118x20.