program main
    implicit none
    integer,parameter :: FTSIZE=8
    real*8,parameter :: M_PI=3.141592653589793238460D0
    integer :: i
    complex*16,dimension(0:FTSIZE-1):: X,B,C
    do i=0,FTSIZE-1
        X(i)=cmplx(1D0/(i+1),1D0/(FTSIZE-i),kind(X(i)));
    end do
    print '("N=",I0)',FTSIZE
    print '("X=")'
    call cvecprint(X);
    call dft(X,B)
    print '("B=")'
    call cvecprint(B)
    call invdft(B,C)
    print '("C=")'
    call cvecprint(C)
    return

contains 

subroutine dft(x,b)
    complex*16,dimension(0:) :: x,b
    integer :: N,l,k
    N=size(x)
    do l=0,N-1
        b(l)=0
    end do
    do k=0,N-1
        do l=0,N-1
            b(k)=b(k)+exp(cmplx(0D0,-2*M_PI*k*l/N,kind(b(k))))*x(l)
        end do
    end do
end subroutine

subroutine invdft(x,b)
    complex*16,dimension(0:) :: x,b
    integer :: N,l,k
    N=size(x)
    do l=0,N-1
        b(l)=0
    end do
    do k=0,N-1
        do l=0,N-1
            b(k)=b(k)+exp(cmplx(0,2*M_PI*k*l/N,kind(b(k))))*x(l)/N;
        end do
    end do
end subroutine

subroutine cvecprint(x)
    complex*16,dimension(0:) :: x
    integer :: N,i
    N=size(x)
    do i=0,N-1
        print '("(",G0," ",G0,")")',x(i)
    end do
end subroutine

end program
