Questions tagged [modular-arithmetic]

Modular arithmetic is quite a useful tool in number theory. In particular, it can be used to obtain information about the solutions (or lack thereof) of a specific equation.

In mathematics, modular arithmetic (sometimes called clock arithmetic) is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value—the modulus. The modern approach to modular arithmetic was developed by Carl Friedrich Gauss in his book Disquisitiones Arithmeticae, published in 1801.

Time-keeping on this clock uses arithmetic modulo 12. A familiar use of modular arithmetic is in the 12-hour clock, in which the day is divided into two 12-hour periods. If the time is 7:00 now, then 8 hours later it will be 3:00. Usual addition would suggest that the later time should be 7 + 8 = 15, but this is not the answer because clock time "wraps around" every 12 hours; in 12-hour time, there is no "15 o'clock". Likewise, if the clock starts at 12:00 (noon) and 21 hours elapse, then the time will be 9:00 the next day, rather than 33:00. Since the hour number starts over after it reaches 12, this is arithmetic modulo 12. 12 is congruent not only to 12 itself, but also to 0, so the time called "12:00" could also be called "0:00", since 0 ≡ 12 mod 12.

324 questions
139
votes
2 answers

Why are λ-calculus optimal evaluators able to compute big modular exponentiations without formulas?

Church numbers are an encoding of natural numbers as functions. (\ f x → (f x)) -- church number 1 (\ f x → (f (f (f x)))) -- church number 3 (\ f x → (f (f (f (f x))))) -- church number 4 Neatly, you can exponentiate 2 church…
23
votes
2 answers

Modulo operator slower than manual implementation?

I have found that manually calculating the % operator on __int128 is significantly faster than the built-in compiler operator. I will show you how to calculate modulo 9, but the method can be used to calculate modulo any other number. First,…
DaBler
  • 2,695
  • 2
  • 26
  • 46
18
votes
1 answer

Modular arithmetics and NTT (finite field DFT) optimizations

I wanted to use NTT for fast squaring (see Fast bignum square computation), but the result is slow even for really big numbers .. more than 12000 bits. So my question is: Is there a way to optimize my NTT transform? I did not mean to speed it by…
Spektre
  • 49,595
  • 11
  • 110
  • 380
13
votes
8 answers

Get the last 1000 digits of 5^1234566789893943

I saw the following interview question on some online forum. What is a good solution for this? Get the last 1000 digits of 5^1234566789893943
J.W.
  • 17,991
  • 7
  • 43
  • 76
11
votes
1 answer

Sympy: Solving Matrices in a finite field

For my project, I need to solve for a matrix X given matrices Y and K. (XY=K) The elements of each matrix must be integers modulo a random 256-bit prime. My first attempt at solving this problem used SymPy's mod_inv(n) function. The problem with…
arnbobo
  • 2,515
  • 2
  • 12
  • 18
10
votes
3 answers

Algorithm to determine if number is between two numbers in modular arithmetic

I'm trying to write a function that answers the question: if you start counting at a and stop counting at b, is c in that range (aka is c between a and b). Normally a < c && c < b would suffice, but I'm in modular arithmetic: Counter-clockwise is…
Kevin Johnson
  • 913
  • 7
  • 24
9
votes
4 answers

How to do arithmetic modulo another number, without overflow?

I'm trying to implement a fast primality test for Rust's u32 and u64 datatypes. As part of it, I need to compute (n*n)%d where n and d are u32 (or u64, respectively). While the result can easily fit in the datatype, I'm at a loss for how to compute…
9
votes
2 answers

Calculating the Modular Inverse in JavaScript

I am trying to take ed = 1 mod((p-1)(q-1)) and solve for d, just like the RSA algorithm. e = 5, (p-1)*(q-1) = 249996 I've tried a lot of code in javascript such as: function modInverse(){ var e = 5; var p = 499; var q = 503; var d =…
Lizziej
  • 95
  • 1
  • 3
9
votes
5 answers

Javascript Modular Arithmetic

Javascript evaluates the following code snippet to -1. -5 % 4 I understand that the remainder theorem states a = bq + r such that 0 ≤ r < b. Given the definition above should the answer not be 3? Why does JavaScript return -1?
9
votes
2 answers

Extremely fast method for modular exponentiation with modulus and exponent of several million digits

As a hobby project I'm taking a crack at finding really large prime numbers. The primality tests for this contain modular exponentiation calculations, i.e. a^e mod n. Let's call this the modpow operation to keep the explanation simple. I am wanting…
8
votes
5 answers

Built-in mod ('%') vs custom mod function: improve the performance of modulus operation

Recently I came to know that the mod('%') operator is very slow. So I made a function which will work just like a%b. But is it faster than the mod operator? Here's my function int mod(int a, int b) { int tmp = a/b; return a - (b*tmp); }
madMDT
  • 448
  • 1
  • 4
  • 16
7
votes
3 answers

Modular arithmetic - competitive programming

I saw a lot of competitive programmers writing code with ((a + b) % d + d) % d in C++. Why don't they just use (a + b) % d? What is that + d inside parentheses good for? Does it have something to do with negative numbers? Thanks
ijkl26
  • 83
  • 6
7
votes
1 answer

Modular equations in Haskell

I want to solve linear and quadratic modular equations in Haskell in one variable. The way I am doing it right now is by putting x = [1..] in the equation one by one and finding the remainder (expr `rem` p == 0, if the equation is modulo p (not…
Iguana
  • 247
  • 1
  • 8
7
votes
2 answers

How can I avoid overflow in modular multiplication?

I know that, (a*b)%m = ((a%m)*(b%m))%m But there is a possibility of overflow. For simplicity lets assume size of integer is 2 bits. If a = 2 (i.e. 102) and b = 2 (i.e. 102), m = 3 (i.e. 112), then a%m and b%m turn out to be 2 and after…
Bhavesh Munot
  • 675
  • 1
  • 6
  • 13
7
votes
2 answers

Using Extended Euclidean Algorithm to create RSA private key

This is for an assignment I'm doing through school. I am having trouble generating a private key. My main problem is understanding the relation of my equations to each other. To set everything up, we have: p = 61 q = 53 n = p * q (which equals…
Niko
  • 4,158
  • 9
  • 46
  • 85
1
2 3
21 22