1

I'm writing a mathematical toolkit consisting of various commands. One of the commands I would like to write is for finding the factors of a 3 digit number. Please name the command as “myfactors”. Here’s an example transcript:

$ myfactors abc
abc is not a number. Please enter a number

$ myfactor 72
72 is not a 3 digit number

$ myfactor 105
The factors are: 1 3 5 7 15 21 35 105
Steven
  • 166,672
  • 24
  • 332
  • 435

1 Answers1

1

Please check this, I used factor GNU tool available In Ubuntu.

#!/bin/bash
num=$1
if [ "$num" -ge 100 ]
then
factor="`factor $num`"
echo "Factor of number $num is $factor"
else
echo "Enter number is not a 3 digit number"
fi

Or you we make it more restrictive to accept only 3 digit numbers

#!/bin/bash
num=$1
if [ "$num" -ge 100 ] && [ "$num" -lt 1000 ]
then
factor="`factor $num`"
echo "Factor of number $num is $factor"
else
echo "Enter number is not a 3 digit number"
fi
Nischay
  • 105
  • 2
  • 11
  • Note that `factor` is not GNU specific. It appeared in Unix V7 (1979). Strangely, there was a `factor` in 4.3BSD, but there's none in any of the modern BSD systems AFAICT (you forgot to quote some of your variables BTW) – Stephane Chazelas Jan 25 '14 at 18:29
  • As per my knowledge factor is a part GNU coreutils. – Nischay Jan 25 '14 at 18:46
  • yes, there's a `factor` in GNU coreutils, (before that in sh-utils). I'm just saying that you find it on Unix systems as well (where it comes from), not only GNU ones. – Stephane Chazelas Jan 25 '14 at 19:27