For a certain subset of problems, it is possible to do this.
For any finite number of clock cycles N, in a Turing-complete language, I can write you a program which terminates but takes longer than N cycles. There is therefore no value of N which will consistently give the correct results in your program.
Turing machines have an infinite tape, so under a Turing machine model, there are infinitely many loop termination conditions, and so it is possible to craft a program which never repeats the same state within N cycles for any N but still terminates.
If your machine is finite state (with no inputs) and s possible states, rather than a Turing machine, you can check if the program halts:
halts s program initialState = do
seenState <- newBooleanArrayOfSize s
setBooleanEntry seenState initialState
halts' initialState
where
halts' state0 =
if state0 == HaltingState
then return True
else
do
seenAlready <- getBooleanEntry seenState state0
if seenAlready
then return False
else
state1 <- nextState program state0
setBooleanEntry seenState state1
halts' state1
In practice, s might be very large (even a 64 bit integer in the state will contribute 2^64 to s) so this algorithm isn't necessarily usable in practice, but it does complete in finite time.