- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- 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.
Use this to learn the idea, then write your own version.
123456 78910111213 14151617 181920 21function nasty (x : int) : boolean22 var f1, f2 : int23 var s : real := sqrt (x)24 var diff : int25 26 f1 := 127 loop28 exit when f1 > s29 30 31 loop32 exit when f1 > s or x mod f1 = 033 f1 := f1 + 134 end loop35 if f1 < s then36 diff := (x div f1) - f137 f2 := f1 + 138 39 40 41 loop42 loop43 exit when f2 > s or x mod f2 = 044 f2 := f2 + 145 end loop46 exit when f2 > s or (x div f2) + f2 <= diff47 f2 := f2 + 148 end loop49 50 51 if f2 < s and x div f2 + f2 = diff then52 result true53 end if54 end if55 56 57 f1 := f1 + 158 end loop59 60 61 result false62end nasty63 64var infile : string := "nasty.in"65var outfile : string := "nasty.out"66var fi, fo : int67var n, x : int68 69open : fi, infile, get70open : fo, outfile, put71 72get : fi, n73for i : 1 .. n74 get : fi, x75 if nasty (x) then76 put : fo, x, " is nasty"77 put x, " is nasty"78 else79 put : fo, x, " is not nasty"80 put x, " is not nasty"81 end if82end for83 84close : fi85close : fo86 87 88 89 90 91