subroutine aptbsum (nbase, idiga, ndiga, itot, nerr)

ccbeg.
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c                             SUBROUTINE APTBSUM
c
c     call aptbsum (nbase, idiga, ndiga, itot, nerr)
c
c     Version:  aptbsum  Updated    2006 May 12 13:40.
c               aptbsum  Originated 2005 August 16 14:20.
c
c     Author:   Arthur L. Edwards, LLNL, L-298, Telephone (925) 422-4123.
c
c
c     Purpose:  For the big integer "a", stored as the base nbase array of
c               ndiga non-negative digits idiga(n), n = 1, ndiga, to find
c               the total number of occurrences itot(nd) of each digit nd from
c               0 to nbase - 1.
c
c               The ndiga digits idiga are the base nbase digits
c               representing the non-negative decimal integer ideca,
c               using one of the equations:
c               ideca = sum (idiga(n) * nbase^(n-1), n = 1, ndiga), or
c               ideca = sum (idiga(n) * nbase^(N-n), n = 1, N = ndiga).
c
c               See aptdtob, aptbadd, aptbsub, aptbmul, aptbdiv, aptbpow,
c               aptbrev, aptbrtn, aptbfac.
c
c     Input:    idiga, ndiga.
c
c     Output:   itot, itots.
c
c     Glossary:
c
c     idiga     Input    The big number "a", stored as an array of ndiga
c                          integers, each representing a single "digit" in the
c                          base nbase representation of the decimal integer
c                          ideca.
c                          If nbase exceeds 10, each "digit" may require 2 or
c                          more integers.  For example, for ideca = 4821
c                          (decimal), and nbase = 16 (hexadecimal),
c                          idiga(n) = (5, 13, 2, 1), with ndiga = 4, or
c                          ideca =  5 * 1 + 13 * 16 + 2 * 256 + 1 * 4096.
c
c     itot      Output   The integer itot(nd) is the number of occurrences of
c                          the integer nd in the array idiga.
c
c     ndiga     Input    The integer ndiga is the number of base nbase digits
c                          in the array idiga.
c
c     nerr      Output   Indicates an input or a result error, if not zero.
c                          1 if nbase is less than 2.
c                          2 if ndiga is negative.
c                          3 if any digits of idiga are negative.
c                          4 if any digits of idiga exceed nbase - 1.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
ccend.

c.... Dimensioned arguments.

      integer idiga(1)
      integer itot(0:1)

c.... Local variables.

      integer n
      integer nbeg
      integer ndiga

cbugc***DEBUG begins.
cbug 9901 format (/ 'aptbsum counting occurrances itot of digits',
cbug     &  ' 0 to nbase,' /
cbug     &  '  in the base nbase integer array idiga, with ndiga',
cbug     &  ' digits.' /
cbug     &  '  nbase =',i7,' ndiga =',i8 )
cbug 9902 format ('bint a          ',1x,i6,' = ',25i2)
cbug      write ( 3, 9901) nbase, ndiga
cbug      if (ndiga .gt. 0) then
cbug        do nin = 1, ndiga, 25
cbug          nlast = min (nin + 24, ndiga)
cbug          write ( 3, 9902) nin, (idiga(n), n = nin, nlast)
cbug        enddo
cbug      endif
cbugc***DEBUG ends.

c.... Test for input errors.

      nerr = 0

      if (nbase .lt. 2) then
        nerr = 1
        go to 210
      endif

      if (ndiga .lt. 0) then
        nerr = 2
        go to 210
      endif

c.... Initialize.

      do nd = 0, nbase - 1
        itot(nd) = 0
      enddo

c.... See if the array idiga exists.

      if (ndiga .eq. 0) then
        go to 210
      endif

c.... Find the number of occurrances of each digit from 0 to nbase - 1.

      if (ndiga .ge. 1) then

        do n = 1, ndiga             ! Looop over big integer digits.

          if (idiga(n) .lt. 0) then
            nerr = 3
            go to 210
          endif

          if (idiga(n) .ge. nbase) then
            nerr = 4
            go to 210
          endif

          nd       = idiga(n)
          itot(nd) = itot(nd) + 1


        enddo                         ! End of loop over big integer digits.

      endif                           ! Big integer has 1 or more digits.

  210 continue

      if (nerr .ne. 0) then
       itot = -999999
      endif
cbugc***DEBUG begins.
cbug 9911 format (/ 'aptbsum results:              nerr=',i2,'.')
cbug 9912 format ('  nd   =',10i5)
cbug 9913 format ('  itot =',10i5)
cbug      write ( 3, 9911) nerr
cbug      if (nbase .le. 10) then
cbug        write ( 3, 9912) (nd,       nd = 0, nbase - 1)
cbug        write ( 3, 9913) (itot(nd), nd = 0, nbase - 1)
cbug      else
cbug        write ( 3, 9912) (nd,       nd = 0, 9)
cbug        write ( 3, 9913) (itot(nd), nd = 0, 9)
cbug        write ( 3, 9912) (nd,       nd = 10, nbase - 1)
cbug        write ( 3, 9913) (itot(nd), nd = 10, nbase - 1)
cbug      endif
cbugc***DEBUG ends.
      return

c.... End of subroutine aptbsum.      (+1 line.)
      end

UCRL-WEB-209832