Looking better now, still few things to mention:
By comparing it like this \'if $calc(xxx) == $int($calc(xxx)) you are actually doing 2 calculations, which slows the script. This script obviously depends on speed, so set a variable, BUT dont use VAR in the loop itself, do it like this:
var %prime, %calc
;restofcode
%calc = $calc($1 / %prime)
if (%calc = $int(%calc))
etc.