Divisibility by 11
InterviewTime limit1sMemory limit128 MB
Apply Dodgson's divisibility-by-11 test to numbers up to 50 digits, printing each intermediate value and the final verdict.
- Level
Medium4 of 10
- Topics
- Math, Implementation, String, Number theory
- Solved
- No attempts yet
Problem
Write a program that reads a positive integer and, using the algorithm described below, determines whether the integer is divisible by 11. This particular test for divisibility by 11 was given in 1897 by Charles L. Dodgson (Lewis Carroll).
Algorithm:
While the number being tested has more than two digits, form a new number by:
- deleting the units digit
- subtracting the deleted digit from the shortened number
The resulting number is divisible by 11 if and only if the original number is divisible by 11.
Input
The first number in the input is the count of the positive integers that follow. Each positive integer has at most 50 digits, and you may assume the integers have no leading zeroes.
Output
For each positive integer, first print the number itself, then print each new value produced by deleting the units digit and subtracting it, one per line (stop once the number has two or fewer digits). Finally, print a sentence stating whether the original number is divisible by 11: use exactly The number X is divisible by 11. when it is divisible and The number X is not divisible by 11. otherwise, where X is the original number. Separate the outputs for different positive integers with a blank line.
Hint
Leading zeroes are not considered part of the number and should not be printed.