I'm trying to create a prime factorization function (recursively), my issue is that the while loop ends after calling the function itself.
Code:
primeFactors(){
if isPrime $1; then
echo "$1"
else
i=2
sqrtnum=`echo "sqrt($1)"|bc`
while [ $i -lt $sqrtnum ]
do
echo "I: $i sqr: $sqrtnum"
j=$(($1%i))
if isPrime $i && [ $j -eq 0 ]
then
echo "$i"
num=$(($1/$i))
primeFactors $num
fi
let i++
done
fi
}
The output is this
I: 2 sqr: 10
2
I: 2 sqr: 7
2
I: 2 sqr: 5
I: 3 sqr 5
I: 4 sqr 5