home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / FACTOR.ZIP / FACTOR.CMD
OS/2 REXX Batch file  |  1991-06-23  |  3KB  |  64 lines

  1. /*                                                                         */
  2. /*      Factors a positive integer.                                        */
  3. /*                                                                         */
  4. /*      Written by James L. Dean                                           */
  5. /*                 406 40th Street                                         */
  6. /*                 New Orleans, LA 70124-1532                              */
  7. /*                                                                         */
  8.    ARG number_to_be_factored
  9.    IF number_to_be_factored = '' THEN
  10.      SAY 'Error:  a positive integer must be specified as an argument.'
  11.    ELSE
  12.      IF VERIFY(number_to_be_factored,'0123456789') = 0 THEN
  13.        DO
  14.          TRUE='1'
  15.          FALSE='0'
  16.          SAY 'Factors:'
  17.          IF number_to_be_factored = 1 THEN
  18.            SAY '          1'
  19.          ELSE
  20.            DO
  21.              IF LENGTH(number_to_be_factored) > 9 THEN
  22.                NUMERIC DIGITS LENGTH(number_to_be_factored)
  23.              left_to_be_factored=number_to_be_factored
  24.              upper_limit=left_to_be_factored
  25.              finished=FALSE
  26.              DO WHILE (\ finished)
  27.                x2=(left_to_be_factored%upper_limit+upper_limit)%2
  28.                IF x2 < upper_limit THEN
  29.                  upper_limit=x2
  30.                ELSE
  31.                  finished=TRUE
  32.              END
  33.              possible_factor=2
  34.              DO WHILE (possible_factor <= upper_limit)
  35.                quotient=left_to_be_factored%possible_factor
  36.                product=quotient*possible_factor
  37.                IF product = left_to_be_factored THEN
  38.                  DO
  39.                    SAY '          '||possible_factor
  40.                    left_to_be_factored=quotient
  41.                    upper_limit=left_to_be_factored
  42.                    finished=FALSE
  43.                    DO WHILE (\ finished)
  44.                      x2=(left_to_be_factored%upper_limit+upper_limit)%2
  45.                      IF x2 < upper_limit THEN
  46.                        upper_limit=x2
  47.                      ELSE
  48.                        finished=TRUE
  49.                    END
  50.                  END
  51.                ELSE
  52.                  IF possible_factor = 2 THEN
  53.                    possible_factor=3
  54.                  ELSE
  55.                    possible_factor=possible_factor+2
  56.              END
  57.              IF left_to_be_factored > 1 THEN
  58.                SAY '          '||left_to_be_factored
  59.            END
  60.        END
  61.      ELSE 
  62.        SAY 'Error:  a positive integer must be specified as an argument.'
  63.       
  64.