/*REXX*/
 /*  TRACE R */
 CALL XGETINFO                       ;/* Get REXX/OS Info */
 TOUT = XFIXS("\mnt\home\TOUT.TXT")  ;/* Define Seq Output File */
                                     ;/* Fix Slashes for OS */
 CALL XFDEL TOUT                     ;/*Delete Output File TOUT*/

 NUMERIC DIGITS 50                   ;

 CALL XCLS                           ;/*Clear Screen Using OS*/

 DO XCNT = 3 TO 3
    CALL NTH XCNT 1000               ;/* Call Routine */
 END

 CALL PRIMES 12999999                ;Regina can do this many
                                     ;Change to lower number till your rexx fails
 TR = LINEOUT(TOUT)                  ;/* Close Ouput File */
EXIT
/*-------------------------------------------------------------------*/
POWER: PROCEDURE
  ARG X,Y
    POW = X**Y
RETURN POW
/*-------------------------------------------------------------------*/
ISPERFECT: PROCEDURE
  ARG N
RETURN 0
/*-------------------------------------------------------------------*/
ISPRIME: PROCEDURE
  ARG LASTNO
  IF LASTNO = 2 THEN RETURN 1
  PRIME. = 1
  DO N = 3 TO LASTNO BY 2
    IF PRIME.N THEN DO
       IF N = LASTNO THEN RETURN 1
       DO K = N TO LASTNO BY N+N
          PRIME.K = 0
       END
    END
  END
RETURN 0
/*-------------------------------------------------------------------*/
PRIMES: PROCEDURE EXPOSE TSPF TOUT
  ARG LASTNO
  STIME = TIME('E')                   ;/* Init Elapsed Timer */
  DCNT. = 0
  CNT = 0
  T = 0
  CALL FSAY "+=======================================================+"
  CALL FSAY "Recap - Solved for Primes Under "LASTNO" - Using "TSPF
  CALL FSAY '2 IS A PRIME NUMBER'
  PRIME. = 1
  DO N = 3 TO LASTNO BY 2
     IF PRIME.N THEN DO
        CALL FSAY N 'IS PRIME'
        T = RIGHT(N,1);DCNT.T = DCNT.T + 1;DCNT.10 = DCNT.10 + 1
        DO K = N TO LASTNO BY N+N
           PRIME.K = 0
        END
     END
  END
  ETIME = TIME('E')                   ;/* Get Elapsed Time */
                                      ;/* Report Header */
  CALL FSAY " "
  CALL FSAY "        Elapsed Time = "ETIME" Seconds"
  CALL FSAY " "
  CALL FSAY DCNT.10" Total Primes Generated"

  DO CNT = 0 TO 9
     T = (DCNT.CNT / DCNT.10) * 100
     CALL FSAY FORMAT(CNT,6,0)"   "FORMAT(DCNT.CNT,15,0)"   "FORMAT(T,4,0)"%"
  END

RETURN
/*-------------------------------------------------------------------*/
/* How many numbers whose cube will equal the root? */
NTH: PROCEDURE EXPOSE TSPF TOUT
  ARG XCNT NCNT
  BCNT. = 0                           ;/* Init Stem Array value all 0 */
  CNT = 0                             ;/* Init Count Var */
  CALL FSAY "+=======================================================+"
  CALL FSAY "Recap - Solved "NCNT" Iterations fo X^ "XCNT" - Using "TSPF
                                      ;/* Perform Iterations */
  STIME = TIME('E')                   ;/* Init Elapsed Timer */
                                      ;/* Perform Iterations */
  DO CNT = 1 TO NCNT
     CCNT = CNT; DO (XCNT-1); CCNT = CCNT * CNT; END
     LCNT = LENGTH(CCNT)              ;/* Determine Length of Cube */
     SCNT = 0                         ;/* Init Summing Variable */
                                      ;/* Sum Digits */
     DO TCNT = 1 TO LCNT
        SCNT = SCNT + SUBSTR(CCNT,TCNT,1) ;/* Sum Each Digit */
     END
                                      ;/* Bingo its a Match */
     IF CNT = SCNT THEN DO
       CALL FSAY "Bingo "CNT"="CCNT" "SCNT
       BCNT.0 = BCNT.0 + 1            ;/* Add to Match Count */
     END
                                      ;/* Not a Match */
     IF CNT <> SCNT THEN DO
        /* CALL FSAY "  NOT "CNT"="CCNT" "SCNT */
        BCNT.SCNT = BCNT.SCNT + 1     ;/* Track not match off count */
     END
  END
                                      ;/* Recap Report */
  ETIME = TIME('E')                   ;/* Get Elapsed Time */
                                      ;/* Report Header */
  CALL FSAY "        Elapsed Time = "ETIME" Seconds"
  CALL FSAY " "
  CALL FSAY "    Number      Occured   Percentage"
                                      ;/* Report # Matches/Not/%*/
  DO CNT = 0 TO 1000
     OP = 0                           ;/* Init Result Var */
     IF BCNT.CNT = 0 THEN
        NOP
     ELSE DO
        OP = ((BCNT.CNT / NCNT) * 100)  ;/* Calculate % of Offness */
        CALL FSAY FORMAT(CNT,10,0)"   "FORMAT(BCNT.CNT,10,0)"   "FORMAT(OP,3,0)"%"
     END
  END

RETURN
/*-------------------------------------------------------------------*/
 FSAY: PROCEDURE EXPOSE TOUT TSPF
   PARSE ARG FVAR                    ;/* Retrieve Passed Argument */
     SAY FVAR                        ;/* Display Data on Screen */
     TR = LINEOUT(TOUT,FVAR)         ;/*DOS*/
 RETURN
/*---------------------------------------------------------------------
  Determines Version of REXX Interpreter
---------------------------------------------------------------------*/
XDETREXX: PROCEDURE
  parse version name level date1 date2 date3
  parse source S1
  /*--- Use this section to display REXX Interpreter Info ---*/
  /*SAY name; SAY level; SAY date1; SAY date2; SAY date3;
      SAY S1;EXIT*/
  /*--  Determine which REXX interpreter is being used ---*/
  TOS = TRANSLATE(WORD(S1,1))
  TSPF = 0                                                    ;/* Stand-alone REXX Interpreter DOSREXX*/
  IF name = "REXXSAA" & date3 = "1994"       THEN TSPF = '0 ' ;/* PCDOS REXX*/
  IF name = "REXX-r4"                        THEN TSPF = '1 ' ;/* R4*/
  IF name = "REXXPC88" & date3 = "1987"      THEN TSPF = '2 ' ;/* REXXIBM (1987) */
  IF name = "REXX/Personal" & date3 = "1992" THEN TSPF = '3 ' ;/* Personal REXX v4 */
  IF name = "REXX/2" & date3 = "1995"        THEN TSPF = '4 ' ;/* SPFPC v4 */
  IF name = "REXX/2" & date3 = "1996"        THEN TSPF = '5 ' ;/* Using SPFPRO */
  IF LEFT(name,11) = "REXX-ooRexx"           THEN TSPF = '6 ' ;/* OOREXX */
  IF LEFT(name,11) = "REXX-Regina"           THEN TSPF = '7 ' ;/* Regina */
  IF name = "REXX" & date3 = "99"            THEN TSPF = '8 ' ;/* Compact REXX*/
  IF name = "brexx" & date3 = "2003"         THEN TSPF = '9 ' ;/* BREXX16 DOS*/
  IF LEFT(name,11) = "REXX:Open-R"           THEN TSPF = '10 ';/* Tritus DOS  */
  IF LEFT(name,11) = "REXX-ooRexx" & TOS = "LINUX" THEN TSPF = '11 ' ;/*OOREXX LINUX*/
  IF LEFT(name,15) = "REXX-Regina_3.6" & TOS = "UNIX" THEN TSPF = '12 '  ;/*Regina LINUX*/
  TSPF = TSPF' 'TOS' 'name
  /*SAY TSPF*/
  TSPF = TSPF" "S1" "ADDRESS()
  /*  CNT = WORDS(S1)
      DO T = 1 TO CNT
        SAY WORD(S1,T)
      END
      SAY " "
      SAY TSPF;PARSE PULL Z  */
RETURN (TSPF)
/*-------------------------------------------------------------------*/
XCMD: PROCEDURE
  PARSE ARG TCMD
  /* SAY TCMD  */
  TSPF = WORD(XDETREXX(),1)                       ;/* Determine Which REXX */
  IF TSPF = 0 THEN ADDRESS COMMAND TCMD           ;/*DOSREXX*/
  IF TSPF = 1 THEN TCMD                           ;/*R4*/
  IF TSPF = 2 THEN ADDRESS DOS TCMD               ;/*REXXPC88*/
  IF TSPF = 3 THEN ADDRESS SYSTEM TCMD            ;/*PERSONAL REXX*/
  IF TSPF = 4 THEN ADDRESS DOS TCMD               ;/*SPFPC4*/
  IF TSPF = 5 THEN ADDRESS DOS TCMD               ;/*SPFPRO*/
  IF TSPF = 6 THEN TCMD                           ;/*OOREXX*/
  IF TSPF = 7 THEN TCMD                           ;/*Regina*/
  IF TSPF = 8 THEN ADDRESS SYSTEM                 ;/*CRX REXX*/
  IF TSPF = 9 THEN ADDRESS SYSTEM                 ;/*BREXX16*/
  IF TSPF = 10 THEN CMD TCMD                      ;/*Tritus DOS*/
  IF TSPF = 11 THEN TCMD                          ;/*OOREXX LINUX*/
  IF TSPF = 12 THEN TCMD                          ;/*Regina 3.6 LINUX*/
RETURN
/*-------------------------------------------------------------------*/
XGETINFO:
 TREXX = XDETREXX()                  ;/* Get REXX and Environment */
 TSPF = WORD(TREXX,1)                ;/* Get REXX Version */
 TOS = WORD(TREXX,2)                 ;/* Get OS - LINUX - DOS - WIN */
RETURN
/*-------------------------------------------------------------------*/
XCLS: PROCEDURE EXPOSE TSPF
  IF (TSPF = 11 | TSPF = 12) THEN
     CALL XCMD "clear"
  ELSE
     CALL XCMD "CLS"
RETURN
/*-------------------------------------------------------------------*/
XFIXS: PROCEDURE EXPOSE TSPF
  PARSE ARG TS
  IF (TSPF = 11 | TSPF = 12) THEN
     TS = TRANSLATE(TS,"/","\")                     ;/*LINUX*/
  ELSE
     TS = TRANSLATE(TS,"\","/")                     ;/*DOS/WIN*/
RETURN TS
/*-------------------------------------------------------------------*/
XFDEL: PROCEDURE EXPOSE TSPF
  PARSE ARG TOUT
  IF (TSPF = 11 | TSPF = 12) THEN
     TCMD = "rm "TOUT                               ;/*LINUX*/
  ELSE
     TCMD ="DEL "TOUT                               ;/*DOS/WIN*/
  CALL XCMD TCMD
RETURN