Skip to content

Instantly share code, notes, and snippets.

@bgschiller
Last active December 21, 2015 15:58
Show Gist options
  • Save bgschiller/6329851 to your computer and use it in GitHub Desktop.
Save bgschiller/6329851 to your computer and use it in GitHub Desktop.
aecho -n "Enter the last number to check for prime-iness: "
read MAX
while [ -n "$(echo ${MAX} | sed -E "s/[0-9]*//g")" -o ${MAX} -ge 274 ]
if [ ${MAX} -ge 274 ]
aecho "Sorry, the line buffers are too short to hold that many numbers."
aecho -n "Please enter a number: "
else
aecho -n "'${MAX}' is not a number. Please enter a number: "
end
read MAX
end
envset IX 2
while [ ${IX} -le ${MAX} ]
envset NUMS "${NUMS} ${IX}"
envset IX $(expr ${IX} + 1)
end
aecho -n "Would you like to display composite numbers as well? (y/n) "
read SHOW_COMP
while [ ${SHOW_COMP} != "y" -a ${SHOW_COMP} != "n" ]
aecho -n "Please enter a 'y' or 'n' "
read SHOW_COMP
end
#While NUMS is not empty
while echo ${NUMS} | grep "[0-9]" > /dev/null
#Copy NUMS to TNUMS, delete NUMS
envset TNUMS "${NUMS}"
envunset NUMS
#set PRIME to the first element of TNUMS
if [ -n "$(echo ${TNUMS} | sed -E "s/[0-9]*\s+(.*)?/\1/g")" ]
envset PRIME "$(echo ${TNUMS} | sed -E "s/([0-9]*)\s+.*/\1/g")"
else
envunset TNUMS
end
#remove the first element of TNUMS
if [ -n "$(echo ${TNUMS} | sed -E "s/[0-9]*\s*(.*)?/\1/g")" ]
envset TNUMS "$(echo ${TNUMS} | sed -E "s/[0-9]*\s+(.*)?/\1/g")"
else
envunset TNUMS
end
echo ${PRIME} is a prime!
#while TNUMS is not empty
while echo ${TNUMS} | grep "[0-9]" > /dev/null
#set CAND to the first element of TNUMS
envset CAND "$(echo ${TNUMS} | sed -E "s/([0-9]*)(\s+.*)?/\1/g")"
#remove the first element of TNUMS
if [ -n "$(echo ${TNUMS} | sed -E "s/[0-9]*\s+(.*)?/\1/g")" ]
envset TNUMS "$(echo ${TNUMS} | sed -E "s/([0-9]*)(\s+.*)?/\2/g")"
else
envunset TNUMS
end
#if PRIME is a factor of CAND
if [ $(expr ${CAND} % ${PRIME}) -eq 0 ]
if [ ${SHOW_COMP} = y ]
echo " ${CAND} is composite: ${PRIME} is a factor"
end
else
envset NUMS "${NUMS} ${CAND}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment