K
K
kr_ilya2021-10-23 13:55:29
Mathematics
kr_ilya, 2021-10-23 13:55:29

Determine the winner of the KNB?

Let there be the following designations for playing stone paper scissors
1 - stone
2 - scissors
3 - paper
It is necessary to determine the winner, by a pair of numbers, for example. (1, 3) is a loss, and (1, 2) is a win.

The article has a way through the remainder of the division. But I don't quite understand why it produces such results.
Here is an excerpt from the article:


0 - well (stone)
1 - scissors 2
- paper

. To determine the winner in a pair, we compare the numbers. (0 - 1) % 3 = 2, then the person lost, (0 - 2) % 3 = 1, then the person won, (2 - 2) % 3 = 0, a draw.


How is it that (0 - 1) % 3 = 2, even if we take the value in parentheses modulo, then 1% 3 = 1, not 2.
Or is the% sign here not getting the remainder of the division?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-10-23
@kr_ilya

(-1) % 3 = 2
This is the mathematical definition of modulo residues. All numbers that differ by n have the same remainder modulo n. The embarrassment is due to the fact that the modulo operation does not work in many programming languages. For negative numbers, it will give a negative value, although you can add n to it to get the correct modulus - a number from 0 to n-1.
Therefore, when implementing, you can do (a-b+3)%3Or transform so that there is no subtraction:b == (a+1)%3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question