4 ms·
Credit card numbers are a good example of the test 'do you need to do math or compare them?' If not, keep the numbers as strings.
by theandrewbailey 6y ago
Credit card numbers are a good example of the test 'do you need to do math or compare them?' If not, keep the numbers as strings.
- bch 6y agoAre the strings checksummed, though? In this case, you’d need to do math, but still keep them as strings...
- PeterisP 6y agoThe checksum calculation does not involve interpreting the whole multidigit number as an integer, so you don't need to parse the number; you're doing math on specific characters/digits only.
- boring_twenties 6y agoWouldn't it in fact be a lot slower using an integer? Lots of divisions by 10, instead of just adding every byte.
- userbinator 6y agohttps://en.wikipedia.org/wiki/Luhn_algorithm https://en.wikipedia.org/wiki/Luhn_algorithm There's exactly one division required. (Implementing it using vector instructions is left as an exercise for the reader. A very elementary exercise, so I encourage you to try it if you're curious about SSE and such.)
- Arnavion 6y agoLuhn's algorithm works on a digit string, not a single integer.
- im3w1l-alt 6y agoChecking divisibility by a known constant can be done in a better way than doing a division. https://lemire.me/blog/2019/02/08/faster-remainders-when-the-divisor-is-a-constant-beating-compilers-and-libdivide/ https://lemire.me/blog/2019/02/08/faster-remainders-when-the...