#!/usr/bin/ksh93
################################################################
#### Program: factor_k93
#### 
#### Description: Emulation of the Unix "factor" command
#### 
#### Author: Dana French (dfrench@mtxia.com)
####         Copyright 2004
#### 
#### Date: 07/26/2004
#### 
################################################################
function factor_k93 {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  integer NUM

  while getopts ":vV" OPTION
  do
    case "${OPTION}" in
        'v') VERBOSE="${TRUE}";;
        'V') VERYVERB="${TRUE}";;
        '?') print "Syntax: factor_k93 [NUMBER]..." && exit 1 ;;
    esac
  done
 
  shift $(( ${OPTIND} - 1 ))

  typeset STDIN="${1:+${FALSE}}"
  STDIN="${STDIN:-${TRUE}}"

#### Read in the data either from STDIN or one or more files
  if (( STDIN == TRUE ))
  then
      while read NUM
      do
          print -n "${NUM}:"
          CNT="2"
          while (( CNT <= NUM ))
          do
              CNT="2"
              while (( ( NUM % CNT ) != 0 )) && (( CNT <= NUM ))
              do
                (( CNT++ ))
              done
              print -n " ${CNT}"
              if (( CNT == NUM ))
              then
                print
                break
              fi
              (( NUM = NUM / CNT ))
        done
    done

  else

    while [[ "_${1}" != "_" ]]
    do
        NUM="${1}"
        print -n "${NUM}:"
        CNT="2"
        while (( CNT <= NUM ))
        do
            CNT="2"
            while (( ( NUM % CNT ) != 0 )) && (( CNT <= NUM ))
            do
                (( CNT++ ))
            done
            print -n " ${CNT}"
            if (( CNT == NUM ))
            then
                print
                break
            fi
            (( NUM = NUM / CNT ))
        done
    shift
    done
  fi

  return 0

}
################################################################

factor_k93 "${@}"