[Question]
  [
# Task
Write a program/function that when given 3 positive integers \$a, b\$ and \$m\$ as input outputs a positive integer \$x\$ such that \$a^x\equiv b\ (\text{mod}\ m)\$ or that no such \$x\$ exists.
A reference implementation can be found [here](https://tio.run/##bZDRboQgEEXf@Yp5ExNqFHBXm9hP6Bc0adyKuyQKBGjqfr1lsE33oRMlmXvPzA24e7xZI/Z9UjNMOnx4FdX7Yq90ZHBhsNqpfCaQarYeNtAG/GiuijaPHpaewdkvHNsOC4YBLn8@Vlr@6Q1s5KF5tUbtenXWRwj3QAgGLdoozEpCFeKkTd6jjWMwmgBDBiofoteOFm@mKKvgFh1pAU8vUJRI/1wgwevoqDaR4YJfjiWqJJkLQWF29PS/ByjzRVLq3tQM8G9rDGnI0aeD11nhh5I@fsZeciLahEuUpOhQ6wRpuuaUZk68ZyDqc0Y5F0SIOumy4TLNiLbPIT0aEvVsihyET/YN "Python 3 – Try It Online").
# Constraints
You can expect \$a\$ and \$b\$ to be less than \$m\$.
# Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so shortest bytes wins.
# Sample Testcases
```
# a, b, m -> x
10, 10, 50 -> 1
10, 100, 200 -> 2
10, 1, 127 -> 42
35, 541, 1438 -> 83
35, 541, 1438 -> 1519
1816, 2629, 3077 -> 223
3306, 4124, 5359 -> 1923
346, 406, 430 -> None
749430, 2427332, 2500918 -> 8025683
3442727916, 3990620294, 6638635157 -> 5731137125
```
**Note:** in the third testcase the solution cannot be 0 since the solution has to be a positive number
  
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~59~~ ~~53~~ 51 bytes
Saved 6 bytes thanks to the man himself [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)!!!
Saved 2 bytes thanks [Dominic van Essen](https://codegolf.stackexchange.com/users/95126/dominic-van-essen)!!!
```
p;x;f(a,b,m){for(p=a,x=1;p-b&&++x<m;)p=p*a%m;x%=m;}
```
[Try it online!](https://tio.run/##bZHbboMwDIbv@xQWEhWUVAsJ9KCU3Ux7irWaAoWNi9AIuIhW8ezMHKJ2XZH8x/D7s5GTrb@yrO@1MKLwJEmJ8q/FpfZ0IolJQqHX6XIZBOaghK8TvZKuEsZNlOh6JcvK8@G6AHyyb1mvoP44QQJX52je2dHs3zBih8D9O3c6MRJl1UKbN@2nnKCQErDBYzx34QYzTgeNNo9Uek8NQiCOUNiG7QlEIYtQ6T9MTVhMCRsptkWJ@A5n0C3mMY8HnNM7MDc6N21@HlEciUOwBGPHMWUo4X5Qy@D@wBvAsjrnBhkq5vQATfmTXwrPtvRf5g9Y7gsIgrHOLtX@gMQe86pG/yT@2Km106e2srZ6ajdo26u/ObpGr/Ac90xgCli/Duo2xwqvVBJICSgCDcFbxx7JbU3TlNPcrVt0/S8 "C (gcc) – Try It Online")
Inputs positive integers \$a,b,m\$ with \$a,b<m\$.  
Outputs a positive integer \$x\$ such that \$a^x\equiv b\ (\text{mod}\ m)\$ or \$0\$ if no such \$x\$ exists.
[Answer]
# [Python 2](https://docs.python.org/2/), ~~55~~ 51 bytes
```
def f(a,b,m,x=1):a**x%m==b<exit(x);x<m<f(a,b,m,x+1)
```
[Try it online!](https://tio.run/##TZBNboMwEIX3PsUoVaVAnQhs8tuQXbe9gwNDagkbZDstnD4dkzSNhJH95nszz@7H8NVZcdWm71wAP3pGa@kxOKwuzuvOttroMM@zNF0n1xobaOaKn7jhQ5kne5Wmw6spy9MBB8KG5H04mMMDecuT597sBbT1AVUNXQOqChfVtiNEa9D2zMETTIJT2iMoC@hc58jlO/hBqEgJ6AOYSxt030bFo2cx1X38nt3NH0OFfaD8JDLWdA5abZHGw3TBUGtLLOWEEweTcGrQYxWwhhKM6uf4rVo@eZa@b6n3bHGcJQl75h5b6p4xCG6kln/vQyg49JSUyCz6YqD/XKA84P4ZwqVB79UZGfRO23Cv8AfxP/GaZxziWmWwOELObmf6iWxSxE2hT2ziuRBMrggvolTIbdS2kuXbfE2etdhxkNlmQoWQTMqM9CIXBXnkajcN2cVCEfWpKKdBn53FXw "Python 2 – Try It Online")
A recursive function that simply tests all exponents from \$1\$ to \$m\$. Returns through exit code: a positive exponent \$x\$, or \$0\$ if no such \$x\$ exists.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~38~~ 33 bytes
Expects `(a,m)(b)` as 3 BigInts. Throws *RangeError* if there's no solution.
```
(a,m,x=m)=>g=b=>a**--x%m-b?g(b):x
```
[Try it online!](https://tio.run/##Xc5Na4NAEAbgu79iLoWdsJr9MtGWtVDooZdeegweVqvWEt1ilmIp@e12TRVCLi/LPPMu82m@zakc2i8X9va9mmo9EUM7OuoOddboQmdmswnD8a4Li8eGFHg/Tg@HAOAAwBmFNWMGOYXtFviVcTanYKuJ6x7weUPsF1MLytiPY@WRK5ksmMgZecJ3FMROpBQk269NIS4qJfOquFAUYhmn6z3pP4NUnkGxS0p/Esz8avsqyIOotsOzKT8IORgKBYUuR9AZ/IIbfnyWtj/ZYxUdbUNq8tQ2L70jBikszw5xnRaICGcojfPfVXhTriJn39zQ9g2Z1844/QE "JavaScript (Node.js) – Try It Online")
---
# [JavaScript (Node.js)](https://nodejs.org), 39 bytes
Expects `(a,m)(b)` as 3 BigInts. Returns *false* if there's no solution.
NB: This version always returns the smallest solution.
```
(a,m,x=0n)=>g=b=>a**++x%m-b?x<m&&g(b):x
```
[Try it online!](https://tio.run/##Tc7NbsIwDAfwe5/Cl6EEAuQT2m3ppEk77LIXqHpIuraASDIBmvr2XcoaCR/@svyzJZ/Mr7k2l@PPbe3Ddzt2ekSGODJo6rEue211aZbL1Wp4cmv7Nry6xaJHFj8P40uVAVQAjBJIqSjUBLZbYA/G6JScJuOPd8CmDb6fTc4oVBwrGZFJkc@YiwlZznYE@I4XBATdp0vO7yoEjSoZlwSUUEX6p/hnEDIySHpPEV@Cib@Cb7M623Th8mGaA0KVIWAJuBqDLqEJ/hrO7eYcetSh92P/6W/IYAJz6zBOU4tjjX8 "JavaScript (Node.js) – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~10~~ 7 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
Lm¹%³k>
```
Inputs in the order \$m,a,b\$; outputs `0` if no \$x\$ is found.
[Try it online](https://tio.run/##yy9OTMpM/f/fJ/fQTtVDm7Pt/v83NDG24DI25TI1MQQA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfWVCaHHEf5/cCNXibLv/Ov@jow0NdIDI1CBWB8I00DEygHF0DI3MgUxjUx1TEyDHxNgCJGFhaKZjZGZkqWNsYA6WNjYw0zExNDLRMTU2tQQJmAD5IDFjg9hYAA).
**Explanation:**
```
L        # Push a list of values `x` in the range [1, (implicit) input `m`]
 m       # Take the (implicit) input `a` to the power of each of these `x`
  ¹%     # Take each modulo the first input `m`
    ³k   # Get the 0-based index of the first occurrence of the third input `b`
         # (-1 if there are none)
      >  # And increase it by 1 to make it a 1-based index
         # (after which it is output implicitly as result)
```
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), 23 bytes
```
{⍺(∊×⍳⍨)(⍺⍺|⍵×⊢)⌂traj⍵}
```
[Try it online!](https://tio.run/##dc89CsJAEAXg3lNMmRTC7s5uTGobbdULrCQK4h9ioaiNgkhwRQuxsrGysxAbS73JXCSuphAkwmNgho8Ho/vtfDjW7V4zH42GUTeMwiSZkLk7tIqfBzJXMmfXsQebKZmbvcUnl9bz4UC37D5LGrTcktlQvHhckJY72uyrlaKdtVK5mmioQwcsAc7eUQzIHIHn6uB0oOGCzv0QBoKlSPxFwEXhQ2S2QQVKWiXR/zAfs6t87oHwRADICmmhENkUkXkguZCgUAXpD8E/Ky19c0z/YF/1Ag "APL (Dyalog Extended) – Try It Online")
Dyalog APL can't handle large integers, so a modulo should be performed after each iteration.
### How it works
```
{⍺(∊×⍳⍨)(⍺⍺|⍵×⊢)⌂traj⍵}  ⍝ dop; ⍵ ⍺ ⍺⍺ ← a b m
        (      )⌂traj    ⍝ Collect all iterations until duplicate is found
                     ⍵   ⍝   starting from a:
            ⍵×⊢          ⍝     Multiply a
         ⍺⍺|             ⍝     Modulo m
 ⍺(  ⍳⍨)                 ⍝ Find the 1-based index of b in the result,
   ∊×                    ⍝   changing to 0 if not found
```
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), 8 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
_╒k▬\%=)
```
Port of [my 05AB1E answer](https://codegolf.stackexchange.com/a/206700/52210), so also:  
Inputs in the order \$m,a,b\$; outputs `0` if no \$x\$ is found.
[Try it online.](https://tio.run/##LYg7DkBAFAD7d4rXKHTvuyjcRCIaJIjGSSScwqlcZC2RTDEzS7eNwzr3Mbb3sU/3eTVZncfohPwCQr8RsBSfApuWqI5uDEpFuiUHlCAVuHqFqhTQWAxMCdVSUHgA)
**Explanation:**
```
_         # Duplicate the first (implicit) input `m`
 ╒        # Pop one and push a list in the range [1, `m`]
  k       # Push the second input `a`
   ▬      # For each value `x` in the list, take `a` to the power `x`
    \     # Swap so the originally duplicated `m` is at the top of the stack
     %    # Take modulo-`m` on each value in the list
      =   # Get the first 0-based index of the value that equals the third (implicit)
          # input `b` (-1 if there are none)
       )  # And increase it by 1 to make it a 1-based index
          # (after which the entire stack joined together is output implicitly as result)
```
[Answer]
# [R](https://www.r-project.org/)+gmp, ~~47~~ 46 bytes
Or only [37 bytes](https://tio.run/##fc7LCsIwEIXhfd@jkIFBJrdehL6FayWptQaaVtK60JePcadCXJ9vfibEydlgwoON/gbF2a19GLbhNC1jFy/3ud/cMjODFj14s/VXdkBzZHzvoSx911mInzfMrDvrxifjBMgJNX03f3ZCQX8FclFndqkBtUpCySaXaHgFKCrRoqQ6G5KUlOJCoZa6zSn1RlShkunjoogv) by requiring input in the form of `bigz` big integer.
```
function(a,b,m)match(T,as.bigz(a)^(1:m)%%m==b)
```
[Try it online!](https://tio.run/##bY7LCsIwFET3@Y/CDVwk77ZC/8K1ktRaA01b0rrQn48RN1qFWc3hDBPT4F208Q59mCk5@6WN3dqdhqlv0uU2tqufRrDoMNBg1/YKB7TLzvn@AZYege8DLYrQNI6mTxk4wxzNvjffNUPB/gHkotzUUqNWGShZbYWKGxRG1ChZ@aNJZlBxoVBLXW@hyuzFZT5BSHoC "R – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 41 bytes
```
->a,b,m,x=0{(a**x+=1)%m==b&&x||x<m&&redo}
```
[Try it online!](https://tio.run/##TY5Ba4MwGIbv/RUf7WbUpjYmsdWydIeywy4dlN2cDF2VFbSKWnCov93GINsgyeF53vf7Ut6inyERw2of4ghnuBGk1UPTbJbCNh4zISJNa7queco0rYzPeT/4MwBkEwzjdQis9mAj/AflQ4nC9B@Wh25HyCfKHNnmI@fMHYXLprhrb@SIDfUwMLJVJUonyRiRktuUyzZzPLXd@7V8lCrB1A@O@TVGs8CKw69vaKGrOyhudQXzRVuCgMQ3a6sq0kutI4wMKwsLXdvV@efFCHpYtOj1iOB2TeOqApkXUPtIjQzgGZIwrWLYSbb@OC8f1oE1FvvD2@n0cnifQz/cAQ "Ruby – Try It Online")
The logic is straightforward: increment the exponent \$x\$ and return it if it satisfies the required equation, otherwise repeat while \$x\$ is less than \$m\$. Outputs the smallest solution for \$x\$, or `false` if no solution exists.
[Answer]
# Java 8, 97 bytes
```
(a,b,m)->{for(int x=0;x++<m;)if(a.modPow(b.valueOf(x),b.valueOf(m)).equals(b))return x;return-1;}
```
\$a\$ and \$b\$ are both `java.math.BigInteger`; \$m\$ and the output \$x\$ are both `int`.  
Outputs `-1` if no \$x\$ is found.
[Try it online.](https://tio.run/##fVHLTsMwELz3K6ycbMWNnFcfCuXAjQMFqUfEYZ06JSVOSuKUoKpXPoBP5EfKJi3QqhWS5fXOzno94yWsob@cv@ziDKqK3EGab3qEpLlRZQKxItM27QAS0yWyHQ3m2blJF7fIWaiSAL8IS972aBZh/7aHW2XApDGZkpxMyI4Cl1yz/vUmKUraUpuJiBrbvtIRSxOKFxbzh@KNSmcNWa3uE9ow/pdoxhz1WkNWUclYqUxd5qSJ9oe@G213UTt0VcsMhx5mr4t0TjRqpDNTpvni8YkA2ws0qjLUFRxXKLpHH2OCe@IM5a43PMb8kIcBooE/OqGO3AH3Bt6Y@2J42uCLAQ9cL@ChH45PKgEW2qIvzgzsRHSs1jXobP4xe69l9l4ZpZ2iNs4KZZospxZMLBtsi0uMEqPGqG2LfH18ovOWnTuXv/fXcGD837pk@J2Hx2533w)
**Explanation:**
```
(a,b,m)->{            // Method with 2 BigInteger & integer parameters and integer return
  for(int x=0;x++<m;) //  Loop `x` in the range (0,m]:
    if(a.modPow(b.valueOf(x),b.valueOf(m))
                      //   If `a` to the power `x`, modulo `m`
       .equals(b))    //   equals `b`:
      return x;       //    Return `x` as result
  return-1;}          //  If the loop has ended without result, return -1 instead
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 19 bytes
Takes in a list of `[A,M,B]`, output is either `X` or `false`. The `[3306, 5359, 4124]` test case times out on TIO, but returns the correct result locally. First Brachylog answer, so probably not the best solution. :-)
```
bhM>.>0&h;.^;M%~t?∧
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/PynD107PzkAtw1ovztpXta7E/lHH8v@qCtGGBjoKpkBsaBDLBeUaGYD5CAFDI3MgAeYamwJZJsYWQE0muEQMLQzNdBSMDcyBuozMjCwhyowNgIKmxqaWOgomhkYmEEEToJiJMdAKEwOz2P@YOv9HAQA "Brachylog – Try It Online")
### How it works
```
bhM>.>0&h;.^;M%~t?∧
bhM                 set the second item to M
   >.>0             output must be between M and 0
       &h           input's first item (A)
         ;.^        A^output
            ;M%     A^output mod M
               ~t?  must unify with the tail from the input (B)
                  ∧ return the output
```
[Answer]
# [Haskell](https://www.haskell.org/), 42 bytes
```
(a#m)b=last$0:[x|x<-[1..m],mod(a^x-b)m==0]
```
[Try it online!](https://tio.run/##TZFbb5tAEIXf@RVTkgdQCMXGzq3ltVKl9CK1Uh8cW1rMBFbeC9ldbCL5v7uz4EskY7Fnzpz5dmiY3aAQh0PErmRcFoJZd509Lfp9//V2MUlTuUykriK26m/LWBZFtjw4tM5CAYsAIJpkCfhnnsXJ5Ux/0@yjQr/p/XjO5@SeeWWWPxwtD5M76ribPiaQZ/cnY56ROptMZ9SQzx@P6syLQyXP4mUQeByIWAJlAjImrkqTT6CDng4Rgysvl6S1nfvjDFxDZBu9AxbDzQ2EL24FoX@L@Cu1fC4gA9eggsHUAwqLEP7UCsNTQ1EcOwZLeZIj2tTHghwL8YsLaTql@zoBraCPQQKleKGk1/3ew54nj6DPCsLfzNpwRLiI3xgXn8IgkIyr030v1X/MKK7qJx8F47eqNCjtYK1lS3tB8Q5bNPz1fbAYfOu4QQLBhm25NhBhWqfAHbQ0HS3siAmpg/C4Jb/rjMIqgR13je7cMYxm@jyD3sRVhRSpNFgtOse1@uIDK01xHuU8n7kjxBgKWya6IYFBqy13fEsn5bBGEyfAVAWWqzUOTUzZHVHxMbJT/K3DBNYNrjcehtW0Hzvm15Sjhm3Amvk7nUkaFG0Kz8zUPkrRHmk2XUJL7hwBOQ0bxNbTd3QpM8T9/f4LHJcIgpMt9Z9XsvbHOGDYeXD4Dw "Haskell – Try It Online")
The function `(a # m) b` returns a positive integer `x` such that `a ^ x == b (mod m)`. If no such `x` exists, it returns `0`. This is done by the brute force method.
[Answer]
# [bc](https://www.gnu.org/software/bc/manual/html_mono/bc.html), 58 50 bytes
```
define f(a,b,m){for(;x<m;)if(a^++x%m==b)return(x)}
```
[Try it online!](https://tio.run/##RYxLCsMgEED3PYWbghIXo6P5kOQqhdoqZGEK0oJQenYzWkrBEd6bx7hbKXcftt2zwK/SySje4ZH4nJc4i43cpevyOa6rE8k/X2nnWXxKCVyBZHUsiNOP6NPwZ3p6qISWOlPZ4NjWo@qp7fUkGcLwjRDIGaUNxWin5kxVzSPdPQA "bc – Try It Online")
This just tries the integers from 1 to `m`, and outputs the first one
which satisfies being a discrete log. Otherwise, the function returns
`0` (default return value).
[Answer]
# [R](https://www.r-project.org/), 61 bytes
```
function(a,b,m){for(i in c(1:m,0))if((T=(a*T)%%m)==b)break;i}
```
[Try it online!](https://tio.run/##bc67DoJAEAXQnq@gIZkxU8y@eGj2L@gNIJiNAsmKlfHb17V0MZnqntyb8eHiHoMft/F8X682TM9l2Ny6QEc9zfiaVg8ud0s@gDjOxIhuAmgtdIcWi2JGa3vs/djdTu79swWCKZ5hzPYxk@R/QEJWSawMGR1Bqzot1KIkWcqGFFe7muKStJCajDJNijra11V8IsvCBw "R – Try It Online")
The base form of R doesn't support arbitrary-precision arithmetic (see [my other 'R+gmp' answer](https://codegolf.stackexchange.com/questions/206694/find-the-discrete-logarithm/206703#206703) for a solution using the 'gmp' library that allows this).  
But, pleasingly, the step-by-step calculation of `(a^x)mod m` comes-out at only 14 bytes longer than the brute-force approach, and it's *much* faster.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 14 bytes
```
ＮθＩ⊕⌕﹪ＸＮ…·¹θθＮ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/98zr6C0xK80Nym1SKNQ05oroCgzr0TDObG4RMMzL7koNTc1ryQ1RcMtMy9Fwzc/pTQnXyMgvxyoGFmjpo4CUHFOaXFmWWpQYl56qoahjkKhpiaIAEkhqQQB6///DY3MFQwNFAz/65blAAA "Charcoal – Try It Online") Link is to verbose version of code. Takes input in the order `m, a, b` and outputs `0` if there is no solution. Explanation:
```
Ｎθ              Store `m`
        …·¹θ    Range from 1 to `m` inclusive
      ＸＮ        Take powers of `a`
     ﹪      θ   Reduce modulo `m`
    ⌕        Ｎ  Find index of `b`
   ⊕            Convert to 1-indexing
  Ｉ             Cast to string
                Implicitly print
```
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 84 bytes
```
\d+
*
"$+"{`,(?=(_+))((_+),)+
,$.1*$3$&
)`^(_+),\1+
$1,
L$`.*(,_+)(,_+)+$(?<=\1)
$#2
```
[Try it online!](https://tio.run/##HcgrDoAwDABQ32OMhrRrs1AQGAgX4AgLjAQEBkFwHH58zBPv3K79WCznuAp4cCjuTkpDT7Mw06eygGIwjw2WwGn6M5oAmsKIKXjSt34Eaej6aAxY1Dlb3apVag8 "Retina – Try It Online") Sadly no test suite as this uses `"$+"`, and I can't figure out how to emulate that with multiple sets of inputs (Retina just crashes when I try). Takes input in the order `m, a, b` and produces no output if there is no solution. Explanation:
```
\d+
*
```
Convert to unary.
```
"$+"{`
)`
```
Repeat `m` times...
```
,(?=(_+))((_+),)+
,$.1*$3$&
```
... multiply the second number by the second last number and insert it between the first two numbers...
```
^(_+),\1+
$1,
```
... and reduce it modulo `m`.
```
L$`.*(,_+)(,_+)+$(?<=\1)
$#2
```
Count the position of the power matching `b` starting from the end.
[Answer]
# [Io](http://iolanguage.org/), 57 bytes
```
method(a,b,m,x :=1;for(i,1,m,if((x=x*a%m)==b,return i))0)
```
[Try it online!](https://tio.run/##jY7PCsIwDIfvPkUvQis9pEn/rMrexMuGKxbcJmPC3n5mgmjxIrQl/T7yS/K4JnGsxXntu/k6XmSjW93rhZk5pXGSWRv@5yTlUi@HZt@rum711M2PaRBZKVBrkga02K4DJe5THubbsHtTfhB@OR8M35Qc99uNW6r@EqYynsM9Ri0IQplGwM4atNxMLhbOburlqVgs2MiEIy0GIuTCAURTbmNZYojbaIoRPAJGHuI9VZ6ccZ811ic "Io – Try It Online")
]