Remove one expensive calculation pow(pow()) out of the loop + minor changes to the if logic.
pow(pow())
if
Results cross-checked with diff-root
diff-root
cc @bnachman @tadej