Logging while drilling (LWD) plays a crucial role in geo-steering, which can determine the formation boundary and resistivity in real time. In this study, an efficient inversion, which can accurately invert formation information in real time on the basis of fast-forward modeling, is presented. In forward modeling, the Gauss–Legendre quadrature combined with the continued fraction method is used to calculate the response of the LWD instrument in a layered formation. In inversion modeling, the Levenberg–Marquardt (LM) algorithm, combined with the line search method of the Armijo criterion, are used to minimize the cost function, and a constraint algorithm is added to ensure the stability of the inversion. A positive and negative sign is added to the distance parameter to determine whether the LWD instrument is located above or below the formation boundary. We have carried out a series of experiments to verify the accuracy of the inversion. The experimental results suggest that the forward algorithm can make the infinite integral of the Bessel function rapidly converge, and accurately obtain the response of the LWD instrument in a layered formation. The inversion can accurately determine the formation resistivity and boundary in real time. This is significant for geological exploration.