c   5.$BDj@QJ,(B
c     $B?9@n(B $BLwBg(B  , 22980138
c      
      program ex5
c
      implicit none      
      integer :: n 
      real(4) :: i4,a4,r4
      real(8) :: i8,a8,r8,l

c-----log2$B$N??CM(B
      l=dlog(2.0d0)
      write(6,*) '$B<+A3BP?t(B log2 $B$N??CM(B l=',l

c-----$BBf7A8x<0$N7W;;(B
      write(6,*)''
      write(6,*)'I=log2 $B$NCM(B'
      write(6,200)
      do n=10,1000,10
         call trapezoid4(n,i4)
         call trapezoid8(n,i8)

         write(6,100) n,i4,i8
      enddo         

c-----$B@dBP8m:9(B
      write(6,*)''
      write(6,*)'$B@dBP8m:9(B '
      write(6,200)
      do n=10,1000,10
         call trapezoid4(n,i4)
         call trapezoid8(n,i8)

         a4=abs(real(l)-i4)
         a8=abs(l-i8)

         write(6,100) n,a4,a8
      enddo

c-----$BAjBP8m:9(B
      write(6,*)''
      write(6,*)'$BAjBP8m:9(B'
      write(6,200)
      do n=10,1000,10
         call trapezoid4(n,i4)
         call trapezoid8(n,i8)

         a4=abs(real(l)-i4)
         a8=abs(l-i8)

         r4=a4/real(l)
         r8=a8/l

         write(6,100) n,r4,r8
      enddo

  100 format(2x,i5,e18.9,d26.18)
  200 format(1x,'$BEyJ,?t(B',1x,'$BC1@:EY(B',11x,'$BG\@:EY(B') 

      stop
      end
c-----$BBf7A8x<0$N%5%V%k!<%A%s(B($BC1@:EY(B)
      subroutine trapezoid4(n,i4)
       implicit none
       integer :: j
       real(4) :: f4,x
       integer,intent(in)  :: n 
       real(4),intent(out) :: i4

       i4=(f4(0.0e0)+f4(1.0e0))/2.0e0
         do j=1,n-1,1
            x=float(j)/float(n)
            i4=i4+f4(x)
         enddo
       i4=i4/float(n)
      return
      end

c-----$BBf7A8x<0$N%5%V%k!<%A%s(B($BG\@:EY(B)
      subroutine trapezoid8(n,i8)
       implicit none
       integer :: j
       real(8) :: f8 , x
       integer,intent(in)  :: n 
       real(8),intent(out) :: i8

       i8=(f8(0.0d0)+f8(1.0d0))/2.0d0
         do j=1,n-1,1
            x=dble(j)/dble(n)
            i8=i8+f8(x)
         enddo
       i8=i8/dble(n)
      return
      end

c-----$B4X?t(B f(x)=1/(1+x) ($BC1@:EY(B)
      function f4(x)
       implicit none
       real(4) :: f4
       real(4),intent(in) :: x
       f4=1.0e0/(1.0e0+x)
      return
      end

c-----$B4X?t(B f(x)=1/(1+x) ($BG\@:EY(B)
      function f8(x)
       implicit none
       real(8) :: f8
       real(8),intent(in) :: x
       f8=1.0d0/(1.0d0+x)
      return
      end
