Thanks for this. I like all the comments and other details.
Is modifying variable “prime_num” in the function legal?
Based on this thread Function in Constraint this might work in some simulators but not all and is mostly not-recommended.
Overall I like the solution very much, and the bonus stuff below, the only issue being the modification of local variable.