program testhorner
  implicit none
  integer,parameter :: n=5
  real*8 :: a(0:n),rac(n),p,pi,pr
  integer :: i,iter,k


  pi=4*atan(1.d0)
! x^5-6*x^4+Pi*x^4-6*Pi*x^3-9*x^3-9*Pi*x^2+14*x^2+14*Pi*x
! racines : 0 , 1, -2, -Pi , 7
  a(0)=0
  a(1)=14*Pi
  a(2)=14-9*pi
  a(3)=-9-6*pi
  a(4)=pi-6
  a(5)=1

  do i=1,n
     rac(i)=i
  end do

  do iter=1,20
     do i=1,n
        call horner(n,a,rac(i),p)
        pr=1
        do k=1,n
           if (k.ne.i) then
              pr=pr*(rac(i)-rac(k))
           end if
        end do
        rac(i)=rac(i)-p/pr
     end do
     write(*,*) 'iter=',iter, 'rac=',rac(1:n)
  end do
  
end program testhorner

! algorithme bete
subroutine bete(n,a,x,p)
  implicit none
  integer :: n,i
  real*8  :: a(0:n),x,p

  p=a(0)
  do i=1,n
     p=p+a(i)*x**i
  end do

end subroutine bete

! algorithme horner
subroutine horner(n,a,x,p)
  implicit none
  integer :: n,i
  real*8  :: a(0:n),x,p

  p=a(n)
  do i=n-1,0,-1
     p=p*x+a(i)
  end do

end subroutine horner
