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
2
votes
1 answer

Calculate C(n, k) combinations for big numbers using modInverse

I want to calculate combinations C(n, k) where n and k could be very large. I tried to do so by using modular inverse as following, but it's not giving correct output even for small numbers. Can anybody tell me where I'm wrong? import…
2
votes
2 answers

Mathematica PowerMod inverse and mpz_powm in C

I have implemented an algorithm in Mathematica that uses PowerMod to find a modular inverse. I now need to implement this algorithm in C, and I've decided to use gmp and its function mpz_powm, which apparently does the same thing. The problem is,…
James Monagan
2
votes
2 answers

Is there any best way(math / C++ trick) of Iterate through forward and reverse within a give range

Using modular arithmetic (or) (%) operator in C++ we can cycle through the successive numbers with a range. For example: if range is 5 (or) modulo 5 then we can cycle through 0 1 2 3 4 0 (5) 1(6) 2(7) 3(8) 4(9) 0(10)............0 1 2 3…
SridharKritha
  • 8,481
  • 2
  • 52
  • 43
2
votes
1 answer

Modular Exponentiation Algorithm in Python

I have created a function to compute large modular exponents. I am aware that this function is built into the python language. My function is incorrect for numbers with larger than 17 digits, and I can't figure out why. Any help is greatly…
Matt Pennington
  • 560
  • 1
  • 8
  • 21
2
votes
1 answer

Calculating constants for CRC32 using PCLMULQDQ

I'm reading through the following paper on how to implement CRC32 efficiently using the PCLMULQDQ instruction introduced in Intel Westmere and AMD Bulldozer: V. Gopal et al. "Fast CRC Computation for Generic Polynomials Using PCLMULQDQ Instruction."…
tdenniston
  • 3,389
  • 2
  • 21
  • 29
2
votes
1 answer

How to code a Modular operation using C that is using 128 bits Binary and mod with a 4 bit polynomial?

Lets say I have: (data) mod (polynomial) 1110 0101 mod 1001 I understand that I will need to shift the polynomial to the left most bit of the data and execute a XOR operation. 1110 0101 1001 and i will get a result of 0111 0101 Then I will…
Anne
  • 123
  • 8
2
votes
3 answers

Criteria for choosing mod value

I have an array Array = {}, size of array is n My constraints are like this: n <= 100000 and Arrayi <=100 I have to find the products of all the elements in the array, I will be given a mod value with which I have to mod the product. The mod will…
user650521
  • 583
  • 1
  • 9
  • 23
2
votes
1 answer

Very large number modulo prime number

I was asked the following question in an interview: How to solve this: ((3000000!)/(30!)^100000)%(any prime no.) I coded the C program for same using brute force, but I am sure that he was not expecting this. Any suggestions for the solutions?
2
votes
2 answers

Optimising code for modular arithmetic

I am trying to calculate below expression for large numbers. Since the value of this expression will be very large, I just need the value of this expression modulus some prime number. Suppose the value of this expression is x and I choose the…
g4ur4v
  • 3,210
  • 5
  • 32
  • 57
1
vote
2 answers

Java modular division

I'm doing some error correcting, and I need to divide two digits under mod 11 in Java. Now this I know, from using a modular calculator: 9/1 mod 11 = 9 2/10 mod 11 = 9 The problem comes in getting Java to calculate this. In Java: (9 / 1) % 11 = 9…
Tony
  • 3,587
  • 8
  • 44
  • 77
1
vote
0 answers

How to solve for X in an equation with modulus?

Fairly new to modular arithmetic and I've searched through several sources and have not figured out how to do this. I have the following equation: s = (k * (h + x * r)) mod q How would one solve for x? I am quite unsure of what to do with the mod…
yurishima
  • 11
  • 1
1
vote
2 answers

Linear independence of vectors in modular arithmetic mod 2 using Python

I would like to check if there is an easy way to implement a check of linear (in)dependence of vectors but using modular arithmetic mod 2. For example, suppose you have the following 3 vectors: v1 = (1,1,0); v2 = (0,1,1); v3 = (1,0,1). If we use…
fcrp
  • 29
  • 3
1
vote
1 answer

Sliding window algorithm to calculate the list of all k-element contiguous subarray products of an array modulo p

I have an array of n positive integers. I want to calculate a list of all contiguous subarray products of size k modulo p. For instance for the following array: a = [3, 12, 5, 2, 3, 7, 4, 3] with k = 3 and p = 12, the ordered list of all k-sized…
1
vote
0 answers

How to perform Arithmetic on Ones Complement Numbers and correct overflow?

For some backstory, I'm making a program that can do arithmetic on ones complement numbers. To do this I'm converting a binary string into a BigInteger and then performing the math using said BigIntegers, and then converting that back into a binary…
1
vote
1 answer

Modular arithmetic does not work in Threshold signature (TSS) implementation

I was implementing threshold signatures protocol described in this paper and I run into a case where it fails and I don't understand reasons why. In the end, u and x should be the same, but they are not. I would appreciate any advice that will help…
Max
  • 2,063
  • 2
  • 17
  • 16