Arif’s Scratch Pad

Prime Factors

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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
}