A new algorithm for calculating the curvature perturbations in stochastic inflation