perm algorithm