Questions tagged [modulus]

The remainder of the quotient of two numbers (usually integers).

847 questions
6
votes
1 answer

Math behind gcc9+ modulus optimizations

Background I was playing with prime numbers in c, when I stumbled upon a new optimization in gcc trunk (will be version 9.x) that optimizes a modulus comparison to 0 into an integer multiply and comparison using magic numbers. In other words…
technosaurus
  • 7,676
  • 1
  • 30
  • 52
6
votes
6 answers

How can this very long if-statement be simplified?

How can this if-statement be simplified? It makes a plus sign: https://i.stack.imgur.com/PtHO1.png If the statement is completed, then a block is set at the x and y coordinates. for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): if…
Bryce Guinta
  • 3,456
  • 1
  • 35
  • 36
6
votes
1 answer

Error: "expression must have integral or unscoped enum type"

So guys I'm new I tried looking up a solution however, I didn't really understand it that well, I'm writing shorthand to understand how they can be replaced with other pieces of code, so I ran across the modulus to which I added however it gives a …
Victor Martins
  • 119
  • 1
  • 1
  • 6
6
votes
4 answers

What's the fastest way to obtain the highest decimal digit of an integer?

What's the fastest way to implement template unsigned highest_decimal_digit(T x); (which returns e.g. 3 for 356431, 7 for 71 and 9 for 9)? The best I can think of is: constexpr-calculate the "middle-size" power of 10 which fits into…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
4 answers

Finding binomial coefficient for large n and k modulo m

I want to compute nCk mod m with following constraints: n<=10^18 k<=10^5 m=10^9+7 I have read this article: Calculating Binomial Coefficient (nCk) for large n & k But here value of m is 1009. Hence using Lucas theorem, we need only to calculate…
6
votes
2 answers

Fast multiplication modulo 2^16 + 1

The IDEA cipher uses multiplication modulo 2^16 + 1. Is there an algorithm to perform this operation without general modulo operator (only modulo 2^16 (truncation))? In the context of IDEA, zero is interpreted as 2^16 (it means zero isn't an…
ciechowoj
  • 914
  • 6
  • 26
6
votes
3 answers

Why modulus of short not correct in release mode?

Modulus of short integers not correct. This is really weird, and already cost me two days. I've narrowed the problematic code as follows (simplified as far as possible): #include #include int foo(short Width, short Height,…
Jedi
  • 882
  • 1
  • 8
  • 24
6
votes
3 answers

Modulo of a 100k-digit number

For numbers n and m I need to evaluate n % m. The catch is n can be as big as 10^100000, m maxes out at 10^18. unsigned long long is about 2^64 (please correct me if I'm wrong) which won't do, then I thought I could read it in array of characters,…
6
votes
3 answers

What does %= mean in Java?

How does %= work in Java? I've been told that it can be used to reassign a value? Grateful if anyone could teach! Thanks! minutes=0; while(true){ minutes++; minutes%=60; }
Daniel
  • 319
  • 2
  • 4
  • 12
6
votes
3 answers

Solving a modular equation (Python)

I have what seems to be an easy problem to solve in Python, but as I am new to python I am unaware on how to solve this. All I am trying to solve is... (x * e) mod k = 1 (where e and k are known values) Is there any simple way of doing this?
Scalahansolo
  • 2,615
  • 6
  • 26
  • 43
5
votes
4 answers

Is there any easy way to do modulus of 2^32 - 1 operation?

I just heard about that x mod (2^32-1) and x / (2^32-1) would be easy, but how? to calculate the formula: xn = (xn-1 + xn-1 / b)mod b. For b = 2^32, its easy, x%(2^32) == x & (2^32-1); and x / (2^32) == x >> 32. (the ^ here is not XOR). How to do…
user955249
5
votes
2 answers

unsigned overflow with modulus operator in C

i encountered a bug in some c code i wrote, and while it was relatively easy to fix, i want to be able to understand the issue underlying it better. essentially what happened is i had two unsigned integers (uint32_t, in fact) that, when the modulus…
david
  • 139
  • 1
  • 1
  • 9
5
votes
2 answers

Why is modulus defined the way it is in programming languages

I'm not asking about the definition but rather why the language creators chose to define modulus with asymmetric behavior in C++. (I think Java too) Suppose I want to find the least number greater than or equal to n that is divisible by f. If n is…
Ted Tool
  • 51
  • 1
5
votes
3 answers

How to calculate modulus of the form (a*b)%c?

How to calculate modulus of the form (a*b)%c? i want to calculate modulus of multiplication of two int numbers where they are almost in the stage of overflow... here c is also int
Kunal
  • 51
  • 2
5
votes
2 answers

Modulus(%) operator in Typescript not working correctly for big numbers

I'm writing a specific validator and needed the modulus of a 16 digit number. Noticed that the operator % doesn't work correctly after 15 digits. I can rewrite my code to check less digits, but I could not find this limitation anywhere in the…
Danilo Petković
  • 51
  • 1
  • 1
  • 5