REM >Director:Menus.System.Factor

REM This factors the number supplied and displays the result in a menu

ON ERROR: ON ERROR OFF: ERROR ERR,REPORT$+" at "+STR$ ERL
SYS "OS_GetEnv" TO in$
SYS "OS_GetEnv" TO a$
n$=FNarg(in$,"-n","")
n%=VAL n$

*Set Director$Menu FactorSub
SYS "Director_Menu"," """" ""<Director$Menu>"" -temp"

IF n%=0 THEN
 SYS "Director_Option","""Syntax error"""
ELSE
 i%=0
 REPEAT
  a%=FNfactor(n%)
  SYS "Director_Option",""""+STR$ a%+""""
  n%=n% DIV a%
  i%+=1
 UNTIL n%=1
 IF i%=1 THEN SYS "Director_Option","""(Prime)"""
ENDIF

SYS "Director_EndMenu"

END

REM This finds the first factor of the number supplied

DEF FNfactor(a%)
LOCAL i%
IF (a% MOD2)=0 THEN =2
FOR i%=3 TO INT(SQR(a%)+1) STEP 2
  IF (a% MOD i%)=0 THEN =i%
NEXT i%
=a%

DEF FNarg(str$,arg$,def$)
LOCAL i,j
i=INSTR(str$,arg$)
IF i=0 THEN =def$
i+=LEN arg$+1
j=INSTR(str$+" <"," ",i)
=MID$(str$,i,j-i)

