[Question]
  [
In my room, I have this geeky clock (click for full size):
[![enter image description here](https://i.stack.imgur.com/lx2F2m.jpg)](https://i.stack.imgur.com/lx2F2.jpg)
Most of these are not difficult to figure out, but the one for 4-o-clock is particularly tricky:
[![two to the power of negative one modulo seven](https://i.stack.imgur.com/Pa22c.gif)](https://i.stack.imgur.com/Pa22c.gif)
Normally, a fraction like 1/2 doesn't make sense in modular arithmetic since only integers are involved. The correct way, then, is to see this as the *inverse* of 2, or to put it another way, [![two to the power of negative one](https://i.stack.imgur.com/Pkoal.gif)](https://i.stack.imgur.com/Pkoal.gif) is that number [![x](https://i.stack.imgur.com/Lzrio.gif)](https://i.stack.imgur.com/Lzrio.gif) where [![two times x equals one](https://i.stack.imgur.com/sIc0U.gif)](https://i.stack.imgur.com/sIc0U.gif). Put this way, a moment's thought will reveal that [![x equals four](https://i.stack.imgur.com/DlQvi.gif)](https://i.stack.imgur.com/DlQvi.gif) because [![two x equals two times four equals eight which is equivalent to one modulo seven](https://i.stack.imgur.com/ZOSq0.gif)](https://i.stack.imgur.com/ZOSq0.gif).
However, simply finding the *multiplicative* inverse would be far too easy as a challenge. So let's bump up the difficulty to exponentiation, or in other words, finding the modular logarithm or discrete logarithm of 2. In this case, 3 is the modular logarithm of 2 with respect to 7. For those of you with number theory/abstract algebra background, this means calculating the multiplicative order of 2 modulo n.
## The Challenge
Given a positive **odd** integer `n` greater than 1, output the smallest positive integer `x` where [![enter image description here](https://i.stack.imgur.com/0a9rg.gif)](https://i.stack.imgur.com/0a9rg.gif).
## Examples
```
n x
3 2 
5 4 
7 3 
9 6 
11 10 
13 12 
15 4 
17 8 
19 18 
21 6 
23 11 
25 20 
27 18 
29 28 
31 5 
33 10 
35 12 
37 36 
39 12 
41 20 
43 14 
45 12 
47 23 
49 21 
51 8 
53 52 
55 20 
57 18 
59 58 
61 60 
63 6 
65 12 
67 66 
69 22 
71 35 
73 9 
75 20 
77 30 
79 39 
81 54 
83 82 
85 8 
87 28 
89 11 
91 12 
93 10 
95 36 
97 48 
99 30 
101 100 
103 51 
105 12 
107 106 
109 36 
111 36 
113 28 
115 44 
117 12 
119 24 
121 110 
123 20 
125 100 
127 7 
129 14 
131 130 
133 18 
135 36 
137 68 
139 138 
141 46 
143 60 
145 28 
147 42 
149 148 
151 15 
153 24 
155 20 
157 52 
159 52 
161 33 
163 162 
165 20 
167 83 
169 156 
171 18 
173 172 
175 60 
177 58 
179 178 
181 180 
183 60 
185 36 
187 40 
189 18 
191 95 
193 96 
195 12 
197 196 
199 99 
201 66 
```
  
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), 6 bytes
```
R2*%i1
```
[Try it online!](http://jelly.tryitonline.net/#code=UjIqJWkx&input=&args=MjAx)
### How it works
```
R2*%i1  Input: n
R       Range; yield [1, ..., n].
 2*     Compute [2**1, ..., 2**n].
   %    Hook; take all powers modulo n.
    i1  Get the index of the first 1.
        Indices are 1-based, so index k corresponds to the natural number k.
```
[Answer]
# Pyth - ~~9~~ 8 bytes
```
f!t.^2TQ
```
[Test Suite](http://pyth.herokuapp.com/?code=f%21t.%5E2TQ&test_suite=1&test_suite_input=3%0A5%0A7%0A9%0A11%0A13%0A15%0A17%0A19%0A21%0A23%0A25%0A197%0A201&debug=0).
`f`ilters from default of 1 till it finds some x such that modular exponentiation with 2 and the input equals 1.
[Answer]
## Python, 32 bytes
```
f=lambda n,t=2:t<2or-~f(n,2*t%n)
```
Starting with 2, doubles modulo n until the result is 1, recursively incrementing each time, and ending with a count of 1 for the initial value of 2.
[Answer]
## Mathematica, 24 bytes
```
2~MultiplicativeOrder~#&
```
I just used a built-in for this.
[Answer]
# APL, 8 bytes
```
1⍳⍨⊢|2*⍳
```
This is a monadic function train that accepts an integer on the right and returns an integer. To call it, assign it to a variable.
Explanation (calling the input `x`):
```
      2*⍳    ⍝ Compute 2^i for each i from 1 to x
   ⊢|        ⍝ Get each element of the resulting array modulo x
1⍳⍨          ⍝ Find the index of the first 1 in this array
```
Note that the result may be incorrect for large inputs since the exponential gets rounded.
[Answer]
# Pyth, 14 bytes
```
VQIq%^2hNQ1hNB
```
Explanation:
```
VQIq%^2hNQ1hNB
                # Implicit, Q = input
VQ              # For N in range(0, Q)
  Iq      1     # If equals 1
    %^2hNQ      # 2^(N + 1) % Q
           hN   # Print (N + 1)
             B  # Break
```
Try it [here](https://pyth.herokuapp.com/?code=VQIq%25%5E2hNQ1hNB&input=201&debug=0)
[Answer]
# JavaScript (ES6), 28 bytes
```
f=(n,t=2)=>t<2||-~f(n,2*t%n)
```
Based on @xnor's brilliant recursive approach.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 11 bytes
Code:
```
DUG2NmX%iNq
```
Explanation:
```
DUG2NmX%iNq
D            # Duplicates the stack, or input when empty
 U           # Assign X to last item of the stack
  G          # For N in range(1, input)
   2Nm       # Calculates 2 ** N
      X      # Pushes X
       %     # Calculates the modulo of the last two items in the stack
        i    # If equals 1 or true, do { Nq }
         N   # Pushes N on top of the stack
          q  # Terminates the program
             # Implicit, nothing has printed, so we print the last item in the stack
```
[Answer]
# Julia, ~~25~~ 24 bytes
```
n->endof(1∪2.^(1:n)%n)
```
This is simple - `2.^(1:n)%n` finds the powers of 2 within the set, `∪` is `union`, but serves as `unique` and returns only one of each unique power (and because it's an infix operator, I can union with 1 to save a byte over the `∪(2.^(1:n)%n)` approach). Then `endof` counts the number of unique powers, because once it hits 1, it'll just repeat the existing powers, so there'll be as many unique values as the power that produces 1.
[Answer]
## Seriously, 14 bytes
```
1,;╗R`╙╜@%`Míu
```
Hex Dump:
```
312c3bbb5260d3bd4025604da175
```
[Try It Online](http://seriouslylang.herokuapp.com/link/code=312c3bbb5260d3bd4025604da175&input=175)
Explanation:
```
 ,;╗           Make 2 copies of input, put 1 in reg0
    R          push [0,1,...,n-1]
     `    `M   map the quoted function over the range
      ╙        do 2^n
       ╜@%     modulo the value in reg0
1           íu Find the 1-index of 1 in the list.
```
[Answer]
## Haskell, 30 bytes
```
n%1=1
n%t=1+n%(2*t`mod`n)
(%2)
```
Helper argument `t` is doubled modulo `n` each step until it equals 1.
[Answer]
# Japt, 17 bytes
```
1oU f@2pX %U¥1} g
```
[Try it online!](http://ethproductions.github.io/japt?v=master&code=MW9VIGZAMnBYICVVpTF9IGc=&input=OQ==)
This would be three bytes shorter if Japt had a "find the first item that matches this condition" function. *Starts work on one*
### How it works
```
1oU f@2pX %U¥1} g   // Implicit: U = input number
1oU                 // Generate a range of numbers from 1 to U.
                    // "Uo" is one byte shorter, but the result would always be 0.
    f@        }     // Filter: keep only the items X that satisfy this condition:
      2pX %U¥1      //  2 to the power of X, mod U, is equal to 1.
                g   // Get the first item in the resulting list.
                    // Implicit: output last expression
```
[Answer]
# PARI/GP, 20 bytes
```
n->znorder(Mod(2,n))
```
[Answer]
# Julia, ~~33~~ 26 bytes
```
n->findfirst(2.^(1:n)%n,1)
```
This is a lambda function that accepts an integer and returns an integer. To call it, assign it to a variable.
We construct an array as 2 raised to each integer power from 1 to `n`, then we find the index of the first 1 in this array.
Saved 7 bytes thanks to Glen O!
[Answer]
## Perl 5, 29 bytes
```
$n=<>;{2**++$_%$n-1?redo:say}
```
[Hat tip.](/a/68033)
[Answer]
# [MATL](https://esolangs.org/wiki/MATL), 13 bytes
```
it:Hw^w\1=f1)
```
Runs on Octave with the [current GitHub commit](https://github.com/lmendo/MATL/commit/77a5eba228ccedec8ac0d58d0f1842aba03858cc) of the compiler.
Works for input up to `51` (due to limitations of the `double` data type).
### Example
```
>> matl it:Hw^w\1=f1)
> 17
8
```
### Explanation
```
i             % input, "N"
t:            % vector from 1 to N
Hw^           % 2 raised to that vector, element-wise
w\            % modulo N
1=            % true if it equals 1, element-wise
f1)           % index of first "true" value
```
[Answer]
# [Unicorn](https://github.com/vihanb/Unicorn), ~~1307 1062~~ 976 bytes
```
( ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 ( 🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 2 ) 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ✨✨✨✨✨✨✨✨✨✨ 2 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤 ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ ( 🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 2 ✨✨✨✨✨✨✨ 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐 ) ) ( 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ 🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ( ) )
```
I'm attempting to make unicorn a serious golfing language but that's a bit difficult...
Hopefully I'll find a way to retain the "unicorn-ness" of the language while making *much* less bytes
---
Picture:
## [enter image description here](https://i.stack.imgur.com/RS3ot.png)
Uses a [custom](https://github.com/vihanb/Unicorn/blob/master/encoding.txt) encoding.
This answer is non-competing because it uses a version of Unicorn made after this language
[Answer]
# 𝔼𝕊𝕄𝕚𝕟, 11 chars / 22 bytes
```
↻2ⁿḁ%ï>1)⧺ḁ
```
`[Try it here (Firefox only).](http://molarmanful.github.io/ESMin/interpreter.html?eval=true&input=25&code=%E2%86%BB2%E2%81%BF%E1%B8%81%25%C3%AF%3E1%29%E2%A7%BA%E1%B8%81)`
Uses a while loop. This is one of the few times a while loop is better than mapping over a range.
# Explanation
```
          // implicit: ï = input, ḁ = 1
↻2ⁿḁ%ï>1) // while 2 to the power of ḁ mod input is greater than 1
  ⧺ḁ      // increment ḁ
          // implicit output
```
[Answer]
# CJam, 15 bytes
```
2qi,:)f#_,f%1#)
```
Peter Taylor saved a byte. Neat!
[Answer]
# Prolog, 55 bytes
**Code:**
```
N*X:-powm(2,X,N)=:=1,write(X);Z is X+1,N*Z.
p(N):-N*1.
```
**Explained:**
```
N*X:-powm(2,X,N)=:=1, % IF 2^X mod N == 1
     write(X)         % Print X
     ;Z is X+1,       % ELSE increase exponent X
     N*Z.             % Recurse
p(N):-N*1.            % Start testing with 2^1
```
**Example:**
```
p(195).
12
```
Try it online [here](http://swish.swi-prolog.org/p/wbJDbkIG.pl)
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-æ`](https://codegolf.meta.stackexchange.com/a/14339/), 9 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
©2pU %N¶1
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LeY&code=qTJwVSAlTrYx&input=MjAx)
[Answer]
# [Python 3](https://docs.python.org/3/), 37 bytes
```
f=lambda n,x=1:2**x%n==1or 1+f(n,x+1)
```
[Try it online!](https://tio.run/##RVPBatwwEL3rK6aGwDoJYceSLCngY3vuqadCcbN2anC9i@2Ay7Lfvn0zMgSMx5p5evP0xrr8W/@cJ3u/983Y/v19aml63hp@rR4ft4epafg8Ez/1B2SfuLyv3bL@emuXbqGGiqKwVJHx5MgEsmQS1WSYiY8IlhhF1ioHingnYoSKBVahzgieKqCrkEuJKgTL5PG2SmS9Ell0wDabdOVYtzlAQO8yxAUCq3EgAbNn6ektedGY2/jcxifyCDWEIFlb0VNnjjpQLStwYBWY0N4ESwnvzBEgREIiaDERUqEgWorARy89Y9BTxKQnTKzEKZ8meT1GCuQASUm5@Cie6QfkssSsho8QfBRTj0n3MRhztNqDxV8xGA7rBnhcyRoOsI4BjlQa/d4CVgcJSa1jeM2qQewWRpsVMgyvdQ2glQ947qQA08U2husqAbY7ae2EURI4AWQh2qxlN5/hvtdfIuUI/9EUEZ1rTexADCFqAYxeWmIOqg2D4CBIzEI1YBheC0AG@YiClErcVcb9OBiK03X@CRljSSISc0lST7vjmAznRCI8psJs6hr/ujE9LoPcABom@rwJL8tlHNZD8XMqyldDuEC04XYIYC@VkkVqmNbDJIteAMs6H3CxSkkMveS@NLQJA9HcDktHP9rxo/s6z2cAi2/tMHan3F/60nW6PVO3Xbq3FfnrdqN2OtH7eaVrv92K0nTj0gndZZbGxfd2WQBsx/GTZHkpyvt/ "Python 3 – Try It Online")
Thanks Maria Miller for saving me 1 byte.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 35 bytes
```
i;f(n){for(i=1;(1<<i++)%n-1;);--i;}
```
Gives accurate results up to \$x=31\$, otherwise the result is 32.
[Try it online!](https://tio.run/##DcixCoAgEADQ3a@QQLjDHGw9@5OWUIwbOkOiRfx2840vuivGMZgyCLZcKvDuCXwIbC0acZ6QnGPqg@XV98kCX@GEqik9PXV2hsWkQ5ZVZ9g8Iqk@fg "C (gcc) – Try It Online")
]