I would like to check which of the two programs will stop:
P1() {
X = 819688;
while (X != 77) {
X = (X*12367+3466) mod 4294967296;
}
}
P2() {
X = 955725;
while (X != 77) {
X = (X*12367+3466) mod 4294967296;
}
}
Since each iteration is kind of a function composition, ultimately leading to a power of the function inside the while loop, I guess Discrete Logarithm could maybe solve the problem.
Any Prolog implementations around solving the problem?
I wrote two versions, one in C++, using dynamic programming/memoizing, and another in Prolog, not using it, so they are not directly comparable.
C++:
This finishes in 7s.
Prolog:
The idea behind the non-memoizing version is to loop only
N+1
steps. If it doesn't terminate before then, it doesn't terminate at all, since there are onlyN
states.The first of these queries takes much longer:
One could do memoizing in Prolog, obviously, using
table
orassertz
, but I think you would run into memory problems much sooner than C++. C++'svector<bool>
uses just 1 bit per element! (Usingunordered_map
here would be a mistake, as it has slower lookup and uses much more memory)