LDP/LDP/guide/docbook/abs-guide/factr.sh

38 lines
828 B
Bash
Raw Normal View History

2002-06-17 13:17:48 +00:00
#!/bin/bash
# factr.sh: Factor a number
MIN=2 # Will not work for number smaller than this.
2008-11-23 22:43:47 +00:00
E_NOARGS=85
E_TOOSMALL=86
2002-06-17 13:17:48 +00:00
if [ -z $1 ]
then
echo "Usage: $0 number"
exit $E_NOARGS
fi
if [ "$1" -lt "$MIN" ]
then
echo "Number to factor must be $MIN or greater."
exit $E_TOOSMALL
fi
# Exercise: Add type checking (to reject non-integer arg).
echo "Factors of $1:"
2008-11-23 22:43:47 +00:00
# -------------------------------------------------------
echo "$1[p]s2[lip/dli%0=1dvsr]s12sid2%0=13sidvsr[dli%0=\
1lrli2+dsi!>.]ds.xd1<2" | dc
2008-11-23 22:43:47 +00:00
# -------------------------------------------------------
# Above code written by Michel Charpentier <charpov@cs.unh.edu>
# (as a one-liner, here broken into two lines for display purposes).
# Used in ABS Guide with permission (thanks!).
2002-06-17 13:17:48 +00:00
2008-11-23 22:43:47 +00:00
exit
# $ sh factr.sh 270138
# 2
# 3
# 11
# 4093