Fortran solution for the first problem