Skip to main content

Series sum 1/1¹+1/2²+… in FORTRAN

Short answer

Loop k from 1 to n, adding 1.0/(k*k) to sum. Use 1.0 for real division. For n=5 sum ≈ 1.463611; for n=10 sum ≈ 1.549768.

Write a program in FORTRAN to evaluate the following series: series = 1/1¹ + 1/2² + 1/3² + ... ... + 1/n²

Reading of the question. The series is written as 1/1¹ + 1/2² + 1/3² + …, whose general term is 1/k² (the first term is 1 whichever way it is read). So the program finds S = Σ 1/k² for k = 1 to n.

      PROGRAM SERIES2
      INTEGER N, K
      REAL S
      PRINT *, 'ENTER NUMBER OF TERMS'
      READ *, N
      S = 0.0
      DO 10 K = 1, N
         S = S + 1.0 / (K * K)
   10 CONTINUE
      WRITE(*,20) N, S
   20 FORMAT(' SUM OF FIRST ', I3, ' TERMS = ', F10.6)
      STOP
      END

K*K is an integer product and 1.0 / (K*K) is real division, so the fraction is not lost.

Sample values (calculated separately): n = 5 gives 1.463611 and n = 10 gives 1.549768. The sum approaches π²/6 = 1.644934 as n grows.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 11 questions →