6 kyu

A Rule of Divisibility by 13

1,980 of 16,216g964

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

Fundamentals
Algorithms
Mathematics

Stats:

CreatedNov 9, 2015
PublishedNov 9, 2015
Warriors Trained63559
Total Skips21928
Total Code Submissions61703
Total Times Completed16216
Ruby Completions335
Python Completions4013
JavaScript Completions2866
Haskell Completions262
C# Completions702
Java Completions1980
CoffeeScript Completions27
Clojure Completions153
Elixir Completions182
C++ Completions1436
TypeScript Completions497
PHP Completions481
Crystal Completions20
F# Completions97
C Completions968
Rust Completions506
Swift Completions374
Go Completions656
Shell Completions77
R Completions112
OCaml Completions46
Kotlin Completions408
Julia Completions48
Fortran Completions16
Scala Completions148
PowerShell Completions37
Nim Completions18
Reason Completions7
Racket Completions32
Forth Completions16
Prolog Completions10
Haxe Completions9
Dart Completions222
Pascal Completions15
Lua Completions54
Perl Completions16
Elm Completions8
D Completions7
Erlang Completions8
Total Stars801
% of votes with a positive feedback rating91% of 2237
Total "Very Satisfied" Votes1866
Total "Somewhat Satisfied" Votes321
Total "Not Satisfied" Votes50
Ad
Contributors
  • g964 Avatar
  • jhoffner Avatar
  • joh_pot Avatar
  • kazk Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • Just4FunCoder Avatar
  • KayleighWasTaken Avatar
Ad