So, is there such a thing as a useful O(1)-space algorithm? I can't think of a single one.
matches(n, m, string):
return matches((1{n}0{m})\*, string)
cannot.Just think about it -- the former is just a DFA, and so of course you can do it in constant space (provided your input stream is abstracted away, or you use a TM.
x++;
would be two examples.
Unknowingly you have allowed me to stumble onto an actual algorithm with O(1) storage pace, that is, one where there are n numbers, and you want to calculated the modulo-k sum. In this case, the algorithm scales logarithmically in the constant parameter k, but O(1) in n.