Which is the best way, in C, to see if a number is divisible by another? I use this:
if (!(a % x)) {
// this will be executed if a is divisible by x
}
Is there anyway which is faster? I know that doing, i.e, 130 % 13 will result into doing 130 / 13 per 10 times. So there are 10 cycles when just one is needed (I just want to know if 130 is divisible by 13).
Thanks!
I know that doing, i.e, 130 % 13 will result into doing 130 / 13 per 10 times
Balderdash. %
does no such thing on any processor I've ever used. It does 130/13
once, and returns the remainder.
Use %
. If your application runs too slowly, profile it and fix whatever is too slow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With