c   6.$BFsJ,K!(B
c     $B?9@n(B $BLwBg(B , 22980138
c
      program ex6
c
      implicit none
      real :: a,b,c,d,x1,x2,epsilon

      a= 1.0e0
      b=-4.0e0
      c=-2.0e0
      d= 8.0e0
      epsilon = 1.0e-3

      write(6,*)'$BFsJ,K!(B'
      write(6,*)'3$B<!J}Dx<0(B f(x)=ax**3+bx**2+cx+d $B$N2r(B'
      write(6,'(a3,f4.1)')'a=',a
      write(6,'(a3,f4.1)')'b=',b
      write(6,'(a3,f4.1)')'c=',c
      write(6,'(a3,f4.1)')'d=',d
      write(6,300) epsilon

      write(6,*)'$B6h4V(B[-5.0,-2.0]'
        x1=-5.0e0
        x2=-2.0e0
        call device2(a,b,c,d,x1,x2,epsilon)

      write(6,*)'$B6h4V(B[-2.0, 0.0]'
        x1=-2.0e0
        x2= 0.0e0
        call device2(a,b,c,d,x1,x2,epsilon)
      write(6,*)'$B6h4V(B[ 0.0, 2.0]'
        x1= 0.0e0
        x2= 2.0e0
        call device2(a,b,c,d,x1,x2,epsilon)

      write(6,*)'$B6h4V(B[ 2.0, 5.0]'
        x1= 2.0e0
        x2= 5.0e0
        call device2(a,b,c,d,x1,x2,epsilon)

      stop
  300 format(1x,'$B<}B+@:EY(B',1x,e18.9)
      end

c-----$BFsJ,K!$G(B3$B<!J}Dx<0(B f(x)=ax**3+bx**2+cx+d $B$N(B
c-----$B2r$HH?I|2s?t$r5a$a$k%5%V%k!<%A%s(B
      subroutine device2(a,b,c,d,x1,x2,epsilon)
       implicit none
       integer :: n
       real :: f,fx1,fx2,fx3,a,b,c,d,x1,x2,x3,epsilon
      
       n=0
       x3=(x1+x2)*0.5e0
       fx1=f(a,b,c,d,x1)
       fx2=f(a,b,c,d,x2)    
       fx3=f(a,b,c,d,x3)  

        if (fx1*fx2 >= 0.0e0) go to 10
        do while (abs(x1-x2) >= epsilon)
          n=n+1
          if (fx1*fx3 <= 0.0e0) then
            x2=x3
            fx2=fx3
          else if (fx1*fx3 > 0.0e0) then
            x1=x3
            fx1=fx3
          end if    
          x3=(x1+x2)*0.5e0
          fx3=f(a,b,c,d,x3)
        enddo

      write(6,100) n
      write(6,200) x3
      go to 20
   10 write(6,*) '$B$3$N6h4V$K2r$O$"$j$^$;$s(B'
   20 write(6,*)''  
  100 format(1x,'$BH?I|2s?t(B',i3) 
  200 format(4x,'$B2r(B',1x,e18.9)
      return
      end

c-----$B4X?t(B f(x)=ax**3+bx**2+cx+d
      function f(a,b,c,d,x)
      implicit none
      real :: f,x,a,b,c,d
      
      f=a*x**3+b*x**2+c*x+d

      return
      end
