Your notebook has a While
loop nested inside a Do
loop, and the body of those loops seems a bit of a horror of coding.
Ideally, one would like to replace the explicit loops with a higher-level Nest
or NestWhile
or similar structure, but with such complex code inside the loop it's very hard to tell what's going on.
Can you break out some steps that are inside the nested loop by encapsulating them in separate functions, then call those functions inside the loop. Yes, that could make running time greater with the explicit Do
and While
, but it would give us a chance at disentangling what's going on here.