How to simplify module arithmetic?

I have

let f = x => x % 4 === 0 ? 0 : 4 - x % 4

But this is a piece of garbage function. Reference.

x will never be negative.


Here is a kind of truth table, or something like that.

x   x % 4     4 - (x % 4)     f(x)
0   0         4               0
1   1         3               3
2   2         2               2
3   3         1               1
4   0         4               0
5   1         3               3
6   2         2               2
7   3         1               1
8   0         4               0
9   1         3               3

I'm trying to find some correlations here, but it's late, and I don't think my brain is working properly. Zzz

What I see in the column f(x)is a kind of inverse module, resulting in loop outputs from 032103210 ... instead of 01230123 ...

I suggest that using Math.maxor Math.minin combination with Math.absmay help & hellip; Probably there x * -1somewhere & hellip;

Can you help me write fso that it does not suck so badly?

+4
3

- :

(4 - (x % 4)) % 4

:

x   x % 4     4 - (x % 4)     (4 - (x % 4)) % 4
0   0         4               0
1   1         3               3
2   2         2               2
3   3         1               1
4   0         4               0
5   1         3               3
6   2         2               2
7   3         1               1
8   0         4               0
9   1         3               3
+2

, modulo. .

var f = x => 2 * (x & 2 ? ~x & 1 : x & 1) + (x & 1)

x   x % 4    4 - (x % 4)       f(x)
0     0         4               0
1     1         3               3
2     2         2               2
3     3         1               1
4     0         4               0
5     1         3               3
6     2         2               2
7     3         1               1
8     0         4               0
9     1         3               3

: , . , . 4, .

 Input    Output
0 : 0 0    0 0
1 : 0 1    1 1
2 : 1 0    1 0
3 : 1 1    0 1

, , 2 ^ 1- - XOR 2 ^ 0 2 ^ 1, 2 * (x & 2 ? ~x & 1 : x & 1) 2 ^ 0 2 ^ 0. (x & 1), ... var f = x => 2 * (x & 2 ? ~x & 1 : x & 1) + (x & 1)

: (foo XOR bar = foo ? !bar : bar)


                                 u       v
                        y        z       z       w
   x       x & 2   ?    ~x       y & 1   x & 1   2 * z  w + v  f(x)
-- ------  ------  ---  -------  ------  ------  -----  -----  ----
0  0000    0000    F    -0001    0001    0000    0      0      0
1  0001    0000    F    -0010    0000    0001    2      3      3
2  0010    0010    T    -0011    0001    0000    2      2      2
3  0011    0010    T    -0100    0000    0001    0      1      1
4  0100    0000    F    -0101    0001    0000    0      0      0
5  0101    0000    F    -0110    0000    0001    2      3      3
7  0110    0010    T    -0111    0001    0000    2      2      2
8  0111    0010    T    -1000    0000    0001    0      1      1
9  1000    0000    F    -1001    0001    0000    0      0      0
+3

Moving Redu using bitwise operators is a bit ahead:

f(x) = -x & 3

Truth Table ™

x       x       -x       3    -x&3    -x&3
-    ----    -----    ----    ----    ----
0    0000     0000    0011    0000       0
1    0001    -0001    0011    0011       3
2    0010    -0010    0011    0010       2
3    0011    -0011    0011    0001       1
4    0100    -0100    0011    0000       0
5    0101    -0101    0011    0011       3
6    0110    -0110    0011    0010       2
7    0111    -0111    0011    0001       1
8    1000    -1000    0011    0000       0
9    1001    -1001    0011    0011       3

var i, 
    f = x => -x & 3;

for (i = 0; i < 20; i++) {
    console.log(i, f(i));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run codeHide result

The original solution with a negative value and a negative bias is 3then modulo and add the same bias again.

f(x) = (-x - 3) % 4 + 3

var i, 
    f = x => (-x - 3) % 4 + 3;

for (i = 0; i < 20; i++) {
    console.log(i, f(i));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run codeHide result
+2
source

Source: https://habr.com/ru/post/1653879/


All Articles