Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can anyone tell me how to solve this equation for lowest $x$

$$a^x \equiv n \mod m$$

other than trying every possible $x$ from $0$ to $m-1$ ($m$ is prime)?

share|cite|improve this question
It is a hard problem, discrete logarithm. As far as I know, one can do better than plain brute force, but no efficient general algorithm is known. – Daniel Fischer Oct 5 '13 at 18:12
How much hard,can you suggest some heuristic etc.what if everything falls into lets say 32 bit range. – Amit Oct 5 '13 at 19:16

This is known as the discrete logarithm problem, and it's a hard problem. By hard, I mean that it's an NP (in the P$\neq$NP sense) problem.

So the short answer is that for anything of any reasonably small size, you should just do trial division.

For slightly larger ones, there are a variety of slightly-faster-but-still-slow algorithms, such as a variant of Pollard's rho algorithm for factoring, or the cutely titled baby-step giant-step algorithm.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.