If have the following code which works for up to 13 (line 2) in a few seconds but when I try and do upper teens and 20 it gets killed after a couple of minutes.divisible_by () {  for ((check=2; check<13; check++)) {    if [ $(($1 % $check)) -ne 0 ]; then      return    fi    }  echo $1 is divisible by all the numbers  exit 0}d=0while true; do  d=$((d+1))  divisible_by $ddoneI tried usingif $(($1 % $check)) -ne 0; thenbut I get 1: command not foundI tried if $(($1 % $check)) -ne 0; thenbut I got 1:command not found and -ne command not foundI tried if [ $($1 % $check) -ne 0 ]; thenbut I got 1:command not found and -ne: unary operator expectedI tried  if [ $1 % $check -ne 0 ]; thenbut I got [: too many arguments
Can I make [ $(($1 % $check)) -ne 0 ]; be more performant and not be killed?
shell script;scripting;performance
Assuming the goal here is to find the lowest number divisible by integers 2 through n, trying to check every integer is massively inefficient. The easiest algorithm I can think of is to use an array of factors that will eventually be multiplied together to get the answer. It starts empty. Then, take each number in sequence, and divide out the factors that are already in our set of unique factors. Whatever is left is added to the set of unique factors, and we know that that set will be divisible by the current number. Since the product of that set contains the minimum set of prime factors that satisfies all necessary divisions, it is guaranteed to be the smallest number divisible by all numbers 2 through n.upto=20uniqueFactors=()for ((i=2; i <= upto; i++)) do  newFactors=$i  for factor in ${uniqueFactors[@]} #loop over array of factors  do    if [ $(($newFactors%$factor)) -eq 0 ]; then      newFactors=$(($newFactors/$factor));    fi;  done  uniqueFactors+=($newFactors);doneproduct=1for factor in ${uniqueFactors[@]}do  product=$(bc <<< $product*$factor) #use bc for numbers outside integer rangedoneecho $productThis calculates the first number divisible by 2-20 (232792560) in 4 milliseconds on my machine. It calculates up to 100 (69720375229712477164533808935312303556800) in 0.425 seconds and 1000 (a number with several hundred decimal places) in 4.813 seconds. If you were to try to iterate over that many numbers, you'd have to be worried about your process getting killed by the heat death of the universe. Optimizing individual lines gives you improvements in percentages or orders of magnitude. Changing your algorithm can give you improvements on a logarithmic scale.