A algorithm of obtaining absolute dose at each tissue depth only by the mathematical calculation of formula is reported. The algorithm is based on integrating the energy flux of the electron generated by X-ray in the range of irradiation field, and the energy spectrum of ray and the secondary scattered ray are considered in this process. In this algorithm, the water phantom in the irradiation field is divided into several thin layers, and the energy flux of the electrons generated by interaction between the ray and thin layer reaching the calculation point is calculated. Finally, the absolute dose of the calculation point can be obtained by accumulating the energy flux contribution of all thin layers. For the X-ray with continuous energy spectrum, the expected mass attenuation coefficient is calculated for obtaining the photon flux at each depth in this process. The absolute dose calculated by this algorithm is verified by Monte Carlo simulation, and the difference between the algorithm and simulation is compensated for by a dose function about multiple scattering photons, and the function shows fast descent and then slow ascent. It is found that the ratio of the dose caused by backscatter to the surface dose, and the relationship among forward scatter, backward scatter and primary ray, and the relationship between the dose and the depth of the secondary scattered rays show a trend of first rising and then declining, and the depth of the peak value deviates from the position of the thin layer. Three-dimensional energy spectra of the secondary photon and the secondary electron are also compared with each other, and the spectrum is a function of particle flux about particle energy and particle direction. From the perspective of Compton effect, the physical meanings of different positions in the three-dimensional energy spectrum of the two particles are explained. It is found that the difference between algorithm percentage depth dose and simulation percentage depth dose is similar to the difference between small irradiation field percentage depth dose and big irradiation field percentage depth dose from simulation, and it is verified that the difference between algorithm and simulation comes from the increase of scattered rays. Finally, the algorithm is applied to the dose calculation of non-uniform phantom, which can accurately reflect the dose distribution characteristics and have less error.