Loop seems to be infinite

I’m porting my prime number generation code to Typst. I’ve already written this in quite a few other languages so the logic should be fairly solid.

But I get a “loop seems to be infinite” error, and I don’t know whether I’ve made an error in the porting or I’m just running into an arbitrary limit.

I can’t find anything about this error in a forum search, and perhaps I’m pushing the Typst language into things it shouldn’t be used for, but this same algorithm runs happily in Lua and PostScript, so I don’t think I’m asking for anything terribly demanding.

I’m attaching a test case which may have other errors in it, I make no promise that “loop seems to be infinite” is the only thing wrong with it. You can see the same algorithm in lots of other languages at codeberg .

My actual question: is there a way to suppress this error and run the code anyway, accepting responsibility for a potential infinite loop?

genprimes.typ (874 Bytes)

Here you might be confusing a = b and let a = b. In the second case, a new variable is defined, shadowing previous variables. Your p in the outer loop never gets overwritten.

1 Like

A while-loop is limited to I think something like 10k iterations. You can use a for-loop with a higher limit and break out of it or nest two while-loops.

Thanks @AprilGrimoire , that does indeed fix the problem in this case.

And @floki thanks for that, I don’t like it but I guess I have to live with it.