      SUBROUTINE LLSQ(M,N,A,KA,B,KB,NB,WK,IWK,IERR)
      DIMENSION A(KA,N),B(KB,NB),WK(N),IWK(N)
      LOGICAL EXIT
C     --------------
      IERR = 0
      IF (1.LT.N .AND. N.LE.M) GO TO 10
      IERR = 1
      RETURN
C
   10 NP1 = N + 1
      CALL ORTHO(M,N,A,KA,WK,IWK,EXIT)
      IF (EXIT) GO TO 20
      IERR = 2
      RETURN
C
   20 DO 22 J = 1,NB
      CALL ORSOL(M,N,A,KA,WK,IWK,B(1,J))
      IF (M .EQ. N) GO TO 22
      RNORM = 0.0
      DO 21 I = NP1,M
   21 RNORM = RNORM + B(I,J)*B(I,J)
      B(NP1,J) = SQRT(RNORM)
   22 CONTINUE
      RETURN
      END
