      program main
      include 'mpif.h'
      parameter(nmax=1000)
      integer n,i,istart,iend
      double precision x(nmax),isum,isum1,isum2,xc,errmax
      integer nprocs,myrank,ierr
      call mpi_init(ierr)
      call mpi_comm_size(MPI_COMM_WORLD,nprocs,ierr)
      call mpi_comm_rank(MPI_COMM_WORLD,myrank,ierr)
      if (myrank.eq.0) n=1000
      call mpi_bcast(n,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
      istart=n*myrank/nprocs+1
      iend=n*(myrank+1)/nprocs
      isum=0.0d0
      do i=istart, iend
         x(i)=dble(i)
         isum=isum+x(i)*x(i)
      end do
      call mpi_allreduce(isum,isum1,1,MPI_DOUBLE_PRECISION,
     *                   MPI_SUM,MPI_COMM_WORLD,ierr)
      isum1=1.0d0/sqrt(isum1)
      do i=istart, iend
         x(i)=x(i)*isum1
      end do
      isum2=dble(n)*(dble(n)+1.0d0)*(2.0d0*dble(n)+1.0d0)/6.0d0
      isum2=1.0d0/sqrt(isum2)
      errmax=0.0d0
      do i=istart, iend
         xc=dble(i)*isum2
         errmax=max(errmax,abs(x(i)-xc))
      end do
      write(6,*) 'myrank =', myrank, ', maximum error =', errmax
      call mpi_finalize(ierr)
      stop
      end
