Add 3
and then Mod 3
to the first set of results:
$ for i in {-5..5}; do printf "%d " $(( (($i % 3) + 3) % 3 )) ; done
1 2 0 1 2 0 1 2 0 1 2
If you know the maximum range, you can just add a significantly large enough multiple of 3 to make all numbers positive before the first modulo operation.
$ for i in {-5..5}; do printf "%d " $(( ($i + 3000000) % 3 )) ; done
However, the first approach is cleaner and more universal.
Lastly, for fun:
positive_mod() {
local dividend=$1
local divisor=$2
printf "%d" $(( (($dividend % $divisor) + $divisor) % $divisor ))
}
for i in {-5..5}; do
printf "%d " $(positive_mod $i 3)
done