Check divisibility by 7

A number of the form 10a + b is divisible by 7 if and only if a – 2b is divisible by 7
Example:

the number 371: 37 – (2×1) = 37 – 2 = 35; 3 – (2 × 5) = 3 – 10 = -7; thus, since -7 is divisible by 7, 371 is divisible by 7.

Solution
return isDivisibleBy7( num / 10 – 2 * ( num – num / 10 * 10 ) );

LOGIC

   10.a + b 
after multiplying by 2, this becomes
   20.a + 2.b 
and then
   21.a - a + 2.b 
Eliminating the multiple of 21 gives
   - a + 2b
and multiplying by -1 gives
    a - 2b

Leave a comment