1

I am trying to code a modular equation solver, say given an equation,

$x^2=180345$ and the modulo is 8, then we can solve it like this-->

$x^2 = 180345 \bmod 8 = 1 \bmod 8$

Now, x can have only the following set of values mod 8:

{0, 1, 2, 3, 4, 5, 6, 7}

You can verify that only x = 1, 3, 5, 7 satisfy x^2 = 1 mod 8 and the other values do not.

A code for this will be as simple as-->

for i in range(1,8):
  if( (i**2) % 8 == 1):
    print(i)

But I am facing problem when it comes to large values.

for example, say this is the case now

$x^{2023}=60272843869801735644577066673$ and modulo is $19806040628566084345385967582$

With the same code that I mentioned it will take a lifetime to solve this equation, but when I use this online modular equation solver it is performing this operation within seconds. Can someone tell me what is the algorithm that they are using?

Turing101
  • 201
  • 1
    Is this an extension of https://math.stackexchange.com/questions/4024008/how-to-solve-for-x-in-modular-arithmetic-when-the-equation-has-exponent?rq=1? – player3236 Feb 13 '21 at 10:30
  • 1
    Check the algorithm section of https://en.wikipedia.org/wiki/Discrete_logarithm, baby step/giant step is quite a popular one to start with. – zwim Feb 13 '21 at 10:36
  • @zwim The OP's problem is taking $k$'th roots, not discrete logarithms. – Bill Dubuque Feb 13 '21 at 11:14

1 Answers1

1

You cannot solve this congruence in a naive way.
I've been trying to implement such algorithms too,
because they are often quite useful in various algorithmic problems and contests.

Probably they are using the Tonelli-Shanks algorithm in that calculator.
Then... if the modulo is not a prime number, you will need to do further work
probably by using the Chinese remainder theorem

Try to implement the algorithm for a prime modulo, that's a good start.

But you do need to know some elementary number theory to go through this.
Not sure if you do. If you don't then you'd better go through some elementary number theory book first. Maybe some book like this one Elementary Number Theory

peter.petrov
  • 12,568