“A divisibility rule is a shorthand way of determining whether a given integer is divisible by a fixed divisor without performing the division, usually by examining its digits.”
wikipedia.com
When you divide the successive powers of 10 by 13 you get the following remainders of the integer divisions:
Then the whole pattern repeats. Hence the following method:
Multiply the right most digit of the number with the left most number in the sequence shown above, the second right most digit to the second left most digit of the number in the sequence. The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary.
Example:
What is the remainder when 1234567 is divided by 13?
class Thirteen {
public static long thirt(long n) {
int[] numbers = {1, 10, 9, 12, 3, 4};
while (n > 99){
long nr = 0;
int i = 0;
while (n > 0){
nr += n%10 * numbers[i++%6];
n /= 10;
}
n = nr;
}
return n;
}
}
class Thirteen {
public static long thirt(long n) {
long previous = n;
long sum = 0;
while (n != 0){
sum += (n % 10);
n = n / 10;
sum += ((n % 10) * 10);
n = n / 10;
sum += ((n % 10) * 9);
n = n / 10;
sum += ((n % 10) * 12);
n = n / 10;
sum += ((n % 10) * 3);
n = n / 10;
sum += ((n % 10) * 4);
n = n / 10;
}
if (sum == previous) return sum;
return thirt(sum);
}
}
class Thirteen {
public static long thirt(long n) {
long sum = 0;
int len = (""+n).length(), i = 0;
for (char c : (""+n).toCharArray()) sum += (c-'0') * new int[]{1,10,9,12,3,4}[(len-i++-1) % 6];
return sum == n ? n : thirt(sum);
}
}