program f90_gint3
    ! Calculation program of pi by Gauss-Legendre rule with 3 Gauss points
    implicit none
    integer::n,i
    real(8)::a,b
    real(8)::h,s,xa,xb,x1,x2,x3,y1,y2,y3
    character(len=20)::dummy
    real(8),parameter::gp1=-0.774596669241483D0,gw1=0.555555555555555D0
    real(8),parameter::gp2= 0.000000000000000D0,gw2=0.888888888888889D0
    real(8),parameter::gp3= 0.774596669241483D0,gw3=0.555555555555555D0
    real(8),parameter::pi=atan(1.0D0)*4.0D0

    call getarg(1,dummy)
    read(dummy,*) n

    a=-1.0D0
    b= 1.0D0
    h=(b-a)/dble(n)

    xa=a
    s=0.0D0
    do i=1,n
        xb=xa+h
        x1=0.5D0*(xb-xa)*gp1+0.5D0*(xa+xb)
        x2=0.5D0*(xb-xa)*gp2+0.5D0*(xa+xb)
        x3=0.5D0*(xb-xa)*gp3+0.5D0*(xa+xb)
        y1=FUNC(x1)
        y2=FUNC(x2)
        y3=FUNC(x3)
        s=s+0.5D0*(xb-xa)*(gw1*y1+gw2*y2+gw3*y3)
        xa=xb
    end do
    write(6,'(i10,2(e20.12))') n,2.0D0*s,2.0D0*s-pi

stop

contains

    real(8) function FUNC(x)
        real(8),intent(in)::x
        real(8)::d
        FUNC=0.0D0
        d=1.0D0-x*x
        if(0.0D0<=d)FUNC=sqrt(d)
    end function FUNC

end program f90_gint3
