What does 1 (mod N) mean?

I am trying to implement a simple algorithm in JavaScript. Wherever I look, it requires the code to work 1 (mod N). As far as I can tell, 1 modulo nothing (or 1%N) equals 1.

What am I missing? Is it always 1, and if so, why not just use 1?

+5
source share
2 answers

The algorithm probably says something like:

x ≑ 1 (mod N)  # x is congruent to 1 (modulo N)

(mod N) , , . , . x ≑ 1 , x 1 . N , 1 x .

x ≑ 1 (mod N) x % N === 1 JavaScript, x . , : , -1 ≑ 1 (mod 2), (-1) % 2 === -1, 1, "" .

, x , :

       x ≑ 1 (mod N)
β‡’  x - 1 ≑ 0 (mod N)

x - 1, 0, , N, modulo:

if ((x - 1) % N === 0) {
    ...
}
+9

modulo (%), ECMA-262 Β§11.5.3. , modulo ECMAScript , :

:

1 % -Infinity returns 1
...
1 % -2 returns 1
1 % -1 returns 0
1 %  0 returns NaN
1 %  1 returns 0
1 %  2 returns 1
...
1 % Infinity returns 1

1 % -1.1 returns 1
1 %  0.1 returns 0.09999999999999995
1 %  0.6 returns 0.4
1 %  0.5 returns 0
1 %  0.4 returns 0.19999999999999996
1 %  0.9 returns 0.09999999999999998
1 %  1.1 returns 1

, , modulo, , . , , .

- , , 0 1 , - , :

if (1 % n) {
  // do this if n is something other than 0 or 1
}
+1

All Articles