Prime Factors

A small piece of bash script that will calculate the prime factors, given a number

factors()
{
    num=$1
    sqrt=`echo "sqrt($num)" | bc`
    for (( i=$sqrt;$i>=2;i-- ))
    do
       if [[ `echo $num % $i | bc` -eq 0 ]]
       then
          echo "$i `echo $num/$i | bc`"
       fi
    done
}

Leave a Reply

Your email address will not be published. Required fields are marked *