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
4
votes
2 answers

Are there any normal mathematical quotient and remainder for `int` somewhere in C++?

Remainder is always positive, for example, remainder(-1, 7) == 6 not -1 And quotient should be rounded towards minus infinity, not towards zero, for example, quotient(-1, 7) == -1 not 0. Are there such functions somewhere in stdlib?
Dims
  • 47,675
  • 117
  • 331
  • 600
4
votes
2 answers

Type safe modular arthmetic no annotation

There's a number of Haskell modular arithmetic modules that implement type safe modular arithmetic with type annotations. Is it possible to pass in a variable in the type annotation? For example in the mod module the following works let x = 4 :: Mod…
4
votes
1 answer

How to calculate sum of this series modulo m fast?

so I came across this problem where I need to calculate this: 1k+(1+p)k+(1+2*p)k+.....+(1+n*p)k % p Where p is prime and k is some number strictly less than p. p is less than 500 and n*p could range upto 109 The only solution I could think is…
anker
  • 63
  • 5
4
votes
1 answer

Set bits combined with exponential modular arithmetics

I got this question yesterday in a challenge. I thought I had coded it correctly and my sample test case was passed. However not even a single test case passed at the backend. Here is my code. Please, someone, help me out. The challenge is over for…
Brij Raj Kishore
  • 1,595
  • 1
  • 11
  • 24
4
votes
1 answer

Calculating modulus for larger numbers in python

My code works fine with small number but when i do it with large numbers it gives running error n = int(input().strip()) a=[] for a_i in range(n): a,n,m = [int(a_temp) for a_temp in input().strip().split(' ')] #n = test cases #a is a number…
Pygirl
  • 12,969
  • 5
  • 30
  • 43
4
votes
4 answers

Modular exponentiation fails for large mod in C++

This is the code I'm using for calculating (n^p)%mod. Unfortunately, it fails for large values of mod (in my case mod = 10000000000ULL) when I call it from main() method. Any idea; why? ull powMod(ull n, ull p, ull mod) { ull ans = 1; n =…
Sai Nikhil
  • 1,237
  • 2
  • 15
  • 39
4
votes
3 answers

Calculate modulo of permutations of repeated integers

I'd like to calculate Ps mod K where Ps is the total number of unique permutations of elements in a set S. The problem is, the set S can have repetitions, so Ps = n! / (f1!f2! ... fn!), where n is the number of elements, and denominator the product…
4
votes
1 answer

Elliptic curve addition in Jacobian coordinates

I try to add two points on an elliptic curve over a prime field, converting these points from affine/to-affine coordinates, but do not manage to get a correct result (the curve I am testing has a=0). Anyone can see what's wrong? // From…
4
votes
2 answers

Mulplication of integers in a range

A is an array containing at most 105 integers. We have to do 2 kinds of operations on this array in log(N) complexity (where, N= number of elements in A). Operation 1, given v,i,j we have to add v to A[k] (i<=k<=j). Operation 2, given i & j…
Bidhan Roy
  • 441
  • 4
  • 14
3
votes
0 answers

Partial Homomorphic Encryption with Haskell

I am working with a fairly simple Paillier partial homomorphic encryption library in Haskell. The API for the library is here - https://hackage.haskell.org/package/Paillier-0.1.0.3/docs/Crypto-Paillier.html This library unfortunately does not handle…
3
votes
1 answer

Proving simple theorem about cases mod 10

I'd like to prove the following lemma: lemma mod10_cases: "P 0 ⟹ P 1 ⟹ P 2 ⟹ P 3 ⟹ P 4 ⟹ P 5 ⟹ P 6 ⟹ P 7 ⟹ P 8 ⟹ P 9 ⟹ P (n mod 10)" but am finding it surprisingly tricky. The lemma feels straightforward; it just says that in order to prove a…
John Wickerson
  • 1,204
  • 12
  • 23
3
votes
2 answers

Unexpected behaviour of sapply() within vectorized function

I wanted to play around with modular arithmetic and programmed some innocently looking function... but got totally surprised by the following unexpected behaviour: crt <- function(x, mods = c(5, 7)) { sapply(mods, \(y) x %% y) } crt <-…
vonjd
  • 4,202
  • 3
  • 44
  • 68
3
votes
0 answers

Is there a trick for calculating powers of two modulo a large prime using 64 bit ints fast?

I just came up with this: // calculates 2^n mod p uint64_t twopow(uint64_t n, uint64_t p) { const constexpr uint64_t bitmask {18446744073709551615ull}; // 2^64-1 uint64_t res = 1 << (n % 64); res *= modpow(1 + (bitmask ^ p), n >> 6, p); …
Derivative
  • 131
  • 3
3
votes
1 answer

Use of Data.Mod for modular exponentiation in Haskell

When using powModfrom the Math.NumberTheory.Powers.Modular library, the Haskell compiler or interpreter gives the following warning: warning: [-Wdeprecations] In the use of ‘powMod’ (imported from Math.NumberTheory.Powers.Modular): Deprecated: "Use…
enrique
  • 492
  • 3
  • 11
3
votes
4 answers

Python: speed up pow(base,exp,mod) for fixed exp and mod, or with vectorization

The bottleneck of my code is the repeated calling of pow(base,exponent,modulus) for very large integers (numpy does not support such large integers, about 100 to 256 bits). However, my exponent and modulus is always the same. Could I somehow utilize…
torpedo
  • 283
  • 2
  • 15
1 2
3
21 22