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
6
votes
3 answers

Three argument pow for arrays

pow accepts a third argument for modulo pow(x, y, z) that is more efficient computation than x ** y % z. How can you do that with arrays? What I've tried: >>> import numpy as np >>> A = np.array(range(10)) >>> pow(A, 23, 13) TypeError: unsupported…
no step on snek
  • 324
  • 1
  • 15
6
votes
1 answer

Speed up large modular multiplication in base 2^8 without multiplier

I am currenty converting the nacl library to risc-v. I already have poly1305 working. I am trying to do this using the risc-v core instruction set, so I don't have a multiplier. The algorithm for Pol1305 is using at the moment ceil(m/16)*17*17 8-bit…
6
votes
2 answers

Modular inverses and unsigned integers

Modular inverses can be computed as follows (from Rosetta Code): #include int mul_inv(int a, int b) { int b0 = b, t, q; int x0 = 0, x1 = 1; if (b == 1) return 1; while (a > 1) { q = a / b; t = b, b = a %…
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
6
votes
1 answer

How to create a typesafe range-limited numeric type?

In Rust, I have need of a numeric type with the property of having a domain symmetric around 0. If a number n is a valid value, then the number -n must also be valid. How would I ensure type-safety during initialization and arithmetic? How would it…
Aaron3468
  • 1,734
  • 16
  • 29
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 modular multiplication modulo prime for linear congruential generator in C

I am trying to implement a random-number generator with Mersenne prime (231-1) as the modulus. The following working code was based on several related posts: How do I extract specific 'n' bits of a 32-bit unsigned integer in C? Fast multiplication…
Sue
  • 277
  • 1
  • 2
  • 9
6
votes
1 answer

Fast algorithm/formula for serial range of modulo of co-prime numbers

In my project, one part of problem is there. But to simplify, here the problem is being formulated. There are two positive co-prime integers: a and b, where a < b. Multiples of a from 1 through b-1 is listed followed by modulus operation by b. a…
RAM
  • 2,413
  • 1
  • 21
  • 33
6
votes
4 answers

Mod division of two integers

I keep getting the error "The operator % is undefined for the argument type(s) Integer, Integer" I am not quite sure why this is happening. I thought that since modular division cannot return decimals that having integer values would be alright.…
user2268305
  • 75
  • 1
  • 1
  • 6
5
votes
0 answers

Maximization of N modulo k when N is fixed, and k<=N

Suppose there are N collectibles. You can call a minimum of 1 person to a maximum of k persons to collect those collectibles for you. The number of people you call would share a number of collectibles equally among them in the highest equity…
5
votes
1 answer

How do i compute a prime power tower modulo to m

Here is the problem - i have been given a prime number P and a number K. I need to compute P ^ P ^ P ... k times modulo to m. Here P is a prime number. (P ^ (P ^ (P ^ P .... k times))) % m Few examples for P = 2, K = 3, m = 3 2 ^ 2 ^ 2 % 3 = 1 for…
Atul
  • 546
  • 4
  • 16
5
votes
6 answers

Binary string remainder 3

-How to find x mod 3 when x is binary number? Not allowed to use conversion to decimal and then using % operator. -eg- if x is 1101 then output should be 1 but do not convert 1101 to 13 and then find by % 3
tcp
  • 289
  • 3
  • 5
  • 15
5
votes
1 answer

Explanation of right to left binary method of modular arithmetic?

I have been studying this link from wikipedia of modulo of a large number, Here is the pseudocode. function modular_pow(base, exponent, modulus) result := 1 while exponent > 0 if (exponent mod 2 == 1): result := (result *…
Tamim Addari
  • 7,591
  • 9
  • 40
  • 59
5
votes
8 answers

Calculating using modulus

I am working on a problem: The user enters 3 tree heights and also the tree height limit. The program then calculates the amount of tree to remove. Sample input: Tree1: 14 Tree2: 7 Tree3: 16 Tree limit: 11 Sample output: Amount to remove:…
user2782378
5
votes
1 answer

Find x in a^x = a (mod n)

I want to calculate am mod n, where n is a prime number, and m is very large. Rather doing this with binary power calculation, I'd like to find such x that ax = a (mod n) and then calculate a(m mod x) mod n. Obviously such x exists for any a,…
Grigor Gevorgyan
  • 6,753
  • 4
  • 35
  • 64
5
votes
1 answer

Specific modular multiplication algorithm

I have 3 large 64 bit numbers: A, B and C. I want to compute: (A x B) mod C considering my registers are 64 bits, i.e. writing a * b actually yields (A x B) mod 2⁶⁴. What is the best way to do it? I am coding in C, but don't think the language is…
lvella
  • 12,754
  • 11
  • 54
  • 106
1
2
3
21 22