A numerical algorithm is presented for analyzing plane-wave diffraction by a doubly periodic grating whose surface profile is corrugated periodically in two directions. The algorithm is based on the Yasuura method: diffracted fields are expanded in terms of Floquet modes for doubly periodic structures, and the expansion coefficients are determined so that approximate solutions satisfy the boundary conditions in the sense of least squares. A numerical technique for solving the resultant large-sized least squares problem is developed. As a result, the present algorithm can be successfully implemented on a desktop computer such as an engineering workstation. This is demonstrated in numerical examples. Further, it is numerically confirmed that the present algorithm enables us to solve the problems of diffraction by a doubly periodic grating made of dielectric or metal in the resonance region (i.e., the wavelength and the size of a unit cell of the grating are of the same order); the wavelength can range from visible to infrared. The algorithm is applicable also to problems in the microwave region.