If both numbers are represented in "base 1", then checking if one number is a multiple of another is trivial with regular expressions.
x=11111 # 5
y=111111111111111 # 15
# Using bash's regular expression operator for clarity
if [[ $y =~ ($x)* ]]; then
echo "y is a multiple of x"
fi
It's almost certain that you don't have your numbers in base 1, and it's an inefficient representation anyway, so you are better off using actual math. Using POSIX arithmetic:
x=5
y=15
z=$((y/x))
if [ $((z * x)) -eq "$y" ]; then
echo "$y is a multiple of $x"
fi
Since POSIX doesn't support floating-point arithmetic, you can check for divisibility by verifying that the quotient z
times the divisor x
is equal to the dividend y
, which means that there was no remainder left over from the division.