A Rule of Divisibility by 13
Description:
From Wikipedia:
"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."
When you divide the successive powers of 10
by 13
you get the following remainders of the integer divisions:
1, 10, 9, 12, 3, 4
because:
10 ^ 0 -> 1 (mod 13)
10 ^ 1 -> 10 (mod 13)
10 ^ 2 -> 9 (mod 13)
10 ^ 3 -> 12 (mod 13)
10 ^ 4 -> 3 (mod 13)
10 ^ 5 -> 4 (mod 13)
(For "mod" you can see: https://en.wikipedia.org/wiki/Modulo_operation)
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 with 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
?
7 6 5 4 3 2 1 (digits of 1234567 from the right)
× × × × × × × (multiplication)
1 10 9 12 3 4 1 (the repeating sequence)
Therefore following the method we get:
7×1 + 6×10 + 5×9 + 4×12 + 3×3 + 2×4 + 1×1 = 178
We repeat the process with the number 178:
8x1 + 7x10 + 1x9 = 87
and again with 87:
7x1 + 8x10 = 87
From now on the sequence is stationary (we always get 87
) and the remainder of 1234567
by 13
is
the same as the remainder of 87
by 13
( i.e 9
).
Task:
Call thirt
the function which processes this sequence of operations on an integer n (>=0)
. thirt
will return the stationary number.
thirt(1234567)
calculates 178, then 87, then 87 and returns 87
.
thirt(321)
calculates 48, 48 and returns 48
Similar Kata:
Stats:
Created | Nov 9, 2015 |
Published | Nov 9, 2015 |
Warriors Trained | 63559 |
Total Skips | 21928 |
Total Code Submissions | 61703 |
Total Times Completed | 16216 |
Ruby Completions | 335 |
Python Completions | 4013 |
JavaScript Completions | 2866 |
Haskell Completions | 262 |
C# Completions | 702 |
Java Completions | 1980 |
CoffeeScript Completions | 27 |
Clojure Completions | 153 |
Elixir Completions | 182 |
C++ Completions | 1436 |
TypeScript Completions | 497 |
PHP Completions | 481 |
Crystal Completions | 20 |
F# Completions | 97 |
C Completions | 968 |
Rust Completions | 506 |
Swift Completions | 374 |
Go Completions | 656 |
Shell Completions | 77 |
R Completions | 112 |
OCaml Completions | 46 |
Kotlin Completions | 408 |
Julia Completions | 48 |
Fortran Completions | 16 |
Scala Completions | 148 |
PowerShell Completions | 37 |
Nim Completions | 18 |
Reason Completions | 7 |
Racket Completions | 32 |
Forth Completions | 16 |
Prolog Completions | 10 |
Haxe Completions | 9 |
Dart Completions | 222 |
Pascal Completions | 15 |
Lua Completions | 54 |
Perl Completions | 16 |
Elm Completions | 8 |
D Completions | 7 |
Erlang Completions | 8 |
Total Stars | 801 |
% of votes with a positive feedback rating | 91% of 2237 |
Total "Very Satisfied" Votes | 1866 |
Total "Somewhat Satisfied" Votes | 321 |
Total "Not Satisfied" Votes | 50 |