Problem solution · Turing

CCC 1997 P2 - Nasty Numbers

CCC 1997 P2 - Nasty Numbers: 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
91 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 1997 P2 - Nasty Numbers, 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

  • 91 lines of Turing from the credited upstream file ccc97s2.t.
  • The implementation visibly relies on sequence storage.
  • 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 1997 P2 - Nasty Numbers · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1997% problem B: Nasty Numbers%% a nasty number has a pair of factors that when subtracted% equals a pair of factors that add together % observation: thinking of 36% the pairs are:% 1, 36% 2, 18% 3, 12% etc. as the 1st factor increases the diff AND sum decrease % therfore starting at 1, find the difference and then look% for the sum DOWN the list!% they only get smaller so they are easy to find. (no arrays needed) % file handling is used, the number of numbers is given% then the numbers themselves function nasty (x : int) : boolean    var f1, f2 : int    var s : real := sqrt (x)    var diff : int     f1 := 1    loop        exit when f1 > s                % find the next factor        loop            exit when f1 > s or x mod f1 = 0            f1 := f1 + 1        end loop        if f1 < s then            diff := (x div f1) - f1            f2 := f1 + 1                    % find pairs after the first pair with a sum >= diff            % (stop if the sum is less than the diff)            loop                loop                    exit when f2 > s or x mod f2 = 0                    f2 := f2 + 1                end loop                exit when f2 > s or (x div f2) + f2 <= diff                f2 := f2 + 1            end loop                        % if you found it great            if f2 < s and x div f2 + f2 = diff then                result true            end if        end if                % otherwise keep going        f1 := f1 + 1    end loop        % if you never find any pairs: false    result falseend nasty var infile : string := "nasty.in"var outfile : string := "nasty.out"var fi, fo : intvar n, x : int open : fi, infile, getopen : fo, outfile, put get : fi, nfor i : 1 .. n    get : fi, x    if nasty (x) then        put : fo, x, " is nasty"        put x, " is nasty"    else        put : fo, x, " is not nasty"        put x, " is not nasty"    end ifend 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 ↗