Questions tagged [mod]

For questions related with the *mod operation*, that can arise in Cryptography, Number Theory, Hashing, etc..

In computing, the modulo operation finds the remainder after division of one number by another (sometimes called modulus). Given two positive numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n.

437 questions
183
votes
5 answers

Mod in Java produces negative numbers

When I calculate int i = -1 % 2 I get -1 in Java. In Python, I get 1 as the result of -1 % 2. What do I have to do to get the same behavior in Java with the modulo function?
Christian
  • 25,249
  • 40
  • 134
  • 225
80
votes
1 answer

C addition using modulus

I came across an intriguing C code that prints A + B, but I have trouble understanding it. Input Format: A B where A, B are integers between 0 and 10 separated by a single space. Code: main( n ) { gets( &n ); printf("%d", n % 85 -…
William Lee
  • 590
  • 4
  • 8
31
votes
3 answers

Rust use vs mod?

I'm struggling to wrap my head around these, use declaration A use declaration creates one or more local name bindings synonymous with some other path. Usually, a use declaration is used to shorten the path required to refer to a module item.…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
22
votes
4 answers

Kotlin negative modulo returns negative value

In Kotlin I have seen that for function a.mod(n), if a is negative the result is negative. Which is not what modulo is supposed to do. What can I do to have always positive modulo? For example: (-2).mod(9) returns -2 and should be 7.
Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
21
votes
1 answer

What is the function to get the quotient and remainder (DIVMOD) for rust?

x86 and likely other architectures provide a method to get the quotient and remainder in a single operation (DIV). Because of this many languages have a DIVMOD combined operation, (like DIVREM in C#, DIVMOD in Python, or with div and div_t in C. How…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
19
votes
4 answers

Calculating 1^X + 2^X + ... + N^X mod 1000000007

Is there any algorithm to calculate (1^x + 2^x + 3^x + ... + n^x) mod 1000000007? Note: a^b is the b-th power of a. The constraints are 1 <= n <= 10^16, 1 <= x <= 1000. So the value of N is very large. I can only solve for O(m log m) if m =…
square1001
  • 1,402
  • 11
  • 26
9
votes
2 answers

What's the point of Raku's 'mod' operator?

I previously incorrectly thought that the % operator returned the remainder and the mod operator returned the modulus (the remainder and modulus are the same when the operands are both positive or both negative but differ when one operand is…
codesections
  • 8,900
  • 16
  • 50
9
votes
4 answers

Determining coefficient of x^m term in (x^2 + x + 1)^n is even or odd

For a given integers n and m, determine that coefficient of x^m term in (x^2+x+1)^n is even or odd? For example, if n=3 and m=4, (x^2+x+1)^3 = x^6 + 3x^5 + [[6x^4]] + 7x^3 + 6x^2 + 3x + 1, so coefficient of x^4 term is 6 (=even). n and m is as…
square1001
  • 1,402
  • 11
  • 26
9
votes
1 answer

Reuse components in angular2 model driven forms

I'm fairly new to angular2 and for the past few days I have been trying to create reusable form components using model driven forms So lets say we have a component componentA.component.ts @Component({ selector: 'common-a', template: ` …
stefanos
  • 260
  • 3
  • 13
8
votes
1 answer

Is there any way to convert smali to java for editing and vice versa

I am currently modding an app.I am beginner till now.I want to ask is there any way to convert smali file to java for editing and vice versa. I searched the whole internet,even xda forum could not give me accurate answer. There are dozen of methods…
Shiraz Ahmad
  • 85
  • 1
  • 1
  • 8
8
votes
3 answers

How to get positive sign for result from mod in bash

When naively using the mod command in bash the residual gets the wrong sign (in my opinion) for negative numerators: If i write: for i in {-5..5}; do echo $(( $i % 3 )) ; done i get the output (as a row) -2 -1 0 -2 -1 0 1 2 0 1 2 How do i achieve…
Mikael Fremling
  • 720
  • 2
  • 8
  • 24
6
votes
1 answer

Is there an elegant, built-in way to do modulo indexing in R?

Currently, I have extract_modulo = function(x, n, fn=`[`) fn(x, (n-1L) %% length(x) + 1L) `%[mod%` = function (x, n) extract_modulo(x, n) And then: seq(12) %[mod% 14 #[1] 2 Is this already built into R somewhere? I would think so, because R has…
Ana Nimbus
  • 635
  • 3
  • 16
6
votes
1 answer

Fibonacci mod number c++

I have the following problem: I should compute a fibonacci number mod another given number. I know about the Pisano period and i am trying to implement it here. This is the code: #include #include long long…
StefanL19
  • 1,476
  • 2
  • 14
  • 29
5
votes
0 answers

C++ How std::fmod avoids round-off errors when calculating mod(a,b) for a which is much bigger than b

I have a small function to calculate mod as follows: double mod(double a, double b){ return a-floor(a/b)*b; } mod(1e15,3) returns 1 correctly, but mod(1e16,3) returns 0 due to numerical round-off errors in the multiplication. However using…
KMot
  • 467
  • 5
  • 13
5
votes
0 answers

how to wrap radians between -pi and pi with mod?

I'd like to wrap radians between -pi/pi. This is the actual code I did, which work: radians > gPIf ? radians - g2PIf : radians < -gPIf ? radians + g2PIf : radians; But I don't like it too much. Tried with mod, but it seems I've problem with…
markzzz
  • 47,390
  • 120
  • 299
  • 507
1
2 3
29 30