Bash Common Prime Divisors
gcd() {
local _n=$1 _m=$2
if (( _n % _m == 0 )); then
echo "$_m"
return
fi
gcd "$_m" $(( _n % _m ))
}
remove_common_prime_divisors() {
local _n=$1 _m=$2 _d
while (( _n != 1 )); do
_d=$(gcd "$_n" "$_m")
if (( _d == 1 )); then
break
fi
_n=$(( _n / _d ))
done
echo "$_n"
}
common_prime_divisors() {
local -n _arrA="$1"
local -n _arrB="$2"
local _counter=0 _i _x _y _d _rx _ry
for _i in "${!_arrA[@]}"; do
_x=${_arrA[$_i]}
_y=${_arrB[$_i]}
_d=$(gcd "$_x" "$_y")
_rx=$(remove_common_prime_divisors "$_x" "$_d")
if (( _rx != 1 )); then
continue
fi
_ry=$(remove_common_prime_divisors "$_y" "$_d")
if (( _ry == 1 )); then
((_counter++))
fi
done
echo "$_counter"
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.