Problem solution · Turing

CCC 1996 P2 - Divisibility by 11

CCC 1996 P2 - Divisibility by 11: a Turing solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
84 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 1996 P2 - Divisibility by 11, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 84 lines of Turing from the credited upstream file ccc96s2.t.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 1996 P2 - Divisibility by 11 · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1996% problem 2 Divisibility by 11 % take the last digit off the number and subtract it from the shortened% number (repeatedly) until the number is 2 digits.% If n is divisible by 11 the original was divisible by 11. % file i/o used.% the first number is the number of numbers.% numbers may be 50 digits long, no leading zeros. var infile : string := "div.in"var outfile : string := "div.out"var fi, fo : intvar n : intvar line : stringvar digit : intvar x : array 1 .. 50 of intvar xn : int open : fi, infile, getopen : fo, outfile, put get : fi, nfor i : 1 .. n     % read the line and convert to integer array    get : fi, line    xn := length (line)    for k : 1 .. xn        x (k) := strint (line (k))    end for     %while the array length is greater than 2    loop         % print the array        for k : 1 .. xn            put : fo, x (k) ..            put x (k) ..        end for        put : fo, ""        put ""        exit when xn <= 2         % do the subtraction: go left from end, borrowing if necessary        digit := x (xn)        xn := xn - 1        for decreasing j : xn .. 1            if digit > x (j) then                x (j) := x (j) + 10                x (j - 1) := x (j - 1) - 1            end if            x (j) := x (j) - digit            digit := 0        end for         % check / remove a leading zero        if x (1) = 0 then            xn := xn - 1            for k : 1 .. xn                x (k) := x (k + 1)            end for        end if    end loop    if xn < 2 then        put : fo, "The number ", line, " is not divisible by 11."        put "The number ", line, " is not divisible by 11."    elsif x (1) = x (2) then        put : fo, "The number ", line, " is divisible by 11."        put "The number ", line, " is divisible by 11."    else        put : fo, "The number ", line, " is not divisible by 11."        put "The number ", line, " is not divisible by 11."    end if    put : fo, ""    put ""end for close : ficlose : fo   

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗