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

In C++, can we create a class for each integer?

I need to create a class NumberModuloN. For each positive integer N, there should be a distinct class. What is the most elegant way to do this? Here are some more details: The data consists of a single integer in the range 0 to N-1. It can be…
3
votes
2 answers

Modular Inverse Built-In, C++

There is a very nice way to find a modular inverse (that is, such b that ab ≡ 1 (mod m) for given a and m) in python-3.8: b = pow(a, -1, m) pow is built-in in python-3.8. Is there something like this in c++?
3
votes
2 answers

Mathematically, why does this SICP algorithm for the exponent of a number modulo another number work?

Section 1.2.6 of SICP gives the following procedure: (define (expmod base exp m) (cond ((= exp 0) 1) ((even? exp) (remainder (square (expmod base (/ exp 2) m)) m)) (else (remainder (* base…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
3
votes
3 answers

Modular operation (%) provides false output

With a function, getNextIdx, I want to receive a new index for an array that depends on the current index and the value of the array at that index. I want the function to return the new index by summing the current index with the value of the array…
Peter
  • 393
  • 1
  • 3
  • 15
3
votes
2 answers

Montgomery multiplication on PC with word-size moduli. Is it worth it?

I'm writing some C code for a research project in number theory, which requires to do a lot of operations in modular arithmetic, with many different moduli. To put it simple: I need to do the operation (a * b) % n many many times. The code is meant…
Norian
  • 31
  • 2
3
votes
1 answer

Maximise the equation which consists of sums of products then performed modulus by a number

I need a formula to calculate the maximum sum of products of variable and constant and the whole sum will be then performed a modulus by some number. X = (C1*x1 + C2*x2 + C3*x3..... )%M, we have to maximise 'X' here, the values of Ci and M is given,…
3
votes
1 answer

Is there a way to limit the print to only true congruences?

I'm working on this code to calculate Fermat's Little Theorem and it works as it should. The only issue that I have is that I would hope for it to be more efficient. Is there a way to limit the print to only true congruences? for i in range…
3
votes
1 answer

How to calculate a modulo of complex numbers?

I am trying to find a way to do (w+xi) % (y+zi) in Python. I have tried cmath but it doesn't seem to have support for this. I have made sure it is mathematically possible and attempted to program it, however it didn't work. The code is below, I…
Evelyn
  • 85
  • 8
3
votes
1 answer

Final subtraction in montgomery modular multiplication for an RSA cryptosystem

I'm confused about how one might supposedly bypass the final subtraction of the modulus in radix-2 montgomery modular multiplication, when used in a modular exponentiation algorithm. The following two papers put forward the conditions for bypassing…
3
votes
1 answer

How to find modular multiplicative inverse in c++

#include #define mx 1000005 #define mod 1000003 using namespace std; long long arr[mx]; int fact() { arr[0]=1; for(int i=1; i
Linkon
  • 1,058
  • 1
  • 12
  • 15
3
votes
1 answer

multiprecision unsigned subtraction in C

I am trying to implement multi-precision unsigned subtraction over a finite field (p=2^191-19) in C, but I cannot figure out how to deal with borrow bit! My operands are represented in radix-2^16 as: typedef unsigned long long T[12]; which means…
A23149577
  • 2,045
  • 2
  • 40
  • 74
3
votes
1 answer

Turn recursive program to iterative

A friend challenged me to write a program that gets three integers (int a, int n, int k) and computes efficiently as possible, a^n mod k I came up with this solution public static int rec(int a,int n,int k) //calc a^n mod k { if(n == 1) …
Oria Gruber
  • 1,513
  • 2
  • 22
  • 44
3
votes
3 answers

Find every K such that arr[i]%K is equal to for each arr[i]

I have an array of M integers. I have to find all possible integers K (given there is at least 1 K)such that: 1) K > 1 2) arr[0]%K = arr[1]%K = arr[2]%K = ... = arr[M-1]%K What is the best algorithm for this problem?
3
votes
1 answer

vigenere cypher in c++

I've tried to create a c++ program which takes some input and encrypts it using a vignere cypher. my input is: the swift brown fox jumps over the lazy dog which, given the key "hello", outputs this: alpdvpjemqvayqnenfxozsgpqalpwgcozfg which…
3
votes
3 answers

How to encrypt an ASCII character with textbook RSA in C#?

I have a problem with calculate of ASCII value with exponent and modulus. I want calculate ASCII value of "K" with RSA algorithm. K in ascii value is 75 c = m^e mod n = 75^41 mod 689 = 316 Then how to make it into source code in C#? I got…
Indra776
  • 41
  • 5