c-----------------------------------------------------------
c Chapter 19: Newton Forms of a Polynomalai(p171)
c-----------------------------------------------------------
c   Name of subroutine: POLY
c
c   Algorithm:Operations on polynomials in power and
c             factorial form.
c
c   input: n=6, A(1)=...=A(5)=0, A(6)=1   
c   complier: f77 poly_2.f
c-------------------------------------------------------------------------

        program driver
        logical gamma
        integer n, a(100)
        integer x0, option, val
        integer i
        gamma=.true.
        option = 6 
        x0=-1

        write(6,10)
10      format('Enter n, which n-1 is the degree of the polynomial:')
        read(5,*),n
        write(6,20)
20      format('Enter the coefficients from X1 to Xn:')
            read(5,*) (a(i),i=1,n)
        call poly(n,a,x0,option,val)
        write(6,50)
50      format('With option = 6, x0 = 1,')
        write(6,60)
60      format('The newton form of the polynomial is:')
            write(6,80)(a(i),i=1,n) 
80          format(1x,10(i5))
        end


c-----Subroutine begins here--------------------------------
      subroutine poly(n,a,x0,option,val)
      integer a(n),x0,option,val,z,w,eps
      logical gamma
      gamma=option.ne.0.and. option.ne.-1
      n1=max0(1,min0(n,option))
      if(option.lt.-1) n1=n
      eps=mod(max0(-option,0),2)
      w=-n*eps
      if(option .gt.-2) w=w+x0
      do 1 m=1,n1
      val=0
      z=w
      do 9 i=m,n
      z= z+eps
      val=a(n+m-i)+z*val
9     if (gamma) a(n+m-i)=val
1     if (option.lt.0) w=w+1
      return
      end


