What's a half on the clock?

25

4

In my room, I have this geeky clock (click for full size):

enter image description here

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

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 is that number x where two times x equals one. Put this way, a moment's thought will reveal that x equals four because two x equals two times four equals eight which is equivalent to one modulo seven.

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.

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 

El'endia Starman

Posted 2015-12-28T23:00:56.100

Reputation: 14 504

1Follow-up challenge: calculate that dot chart value thingy – Conor O'Brien – 2015-12-28T23:53:32.167

3@CᴏɴᴏʀO'Bʀɪᴇɴ: That's just binary. – El'endia Starman – 2015-12-29T00:18:45.910

2Graphical input! – Conor O'Brien – 2015-12-29T00:19:28.583

1https://oeis.org/A002326 – msh210 – 2015-12-29T07:05:42.117

Regarding the clock, most of these make sense to me, but not 1, 4, or 5. Your explanation for 4 just confirms what I suspect: it's really a one-way hash function: if you know the value, you can confirm it with the formula. The explanation (assuming it is the correct one) yields multiple possible values for x. Is it 4 o'clock or 11 o'clock? – Otheus – 2015-12-29T10:05:43.983

2

@Otheus: 1 is apparently supposed to be Legendre's constant, but in a strange notation, and 5 is due to phi. Regarding the multiple possible values for x, 11 is equivalent to 4 modulo 7, so they're really the same.

– El'endia Starman – 2015-12-29T10:26:57.407

We all know (1/2)%7=1/2. – SuperJedi224 – 2015-12-29T13:33:55.880

2@SuperJedi224 Who said that that 2^-1 == 1/2? – Dennis – 2015-12-29T15:58:38.157

@Dennis Properties of exponents. Unless that's bitwise XOR, of course. – SuperJedi224 – 2015-12-29T16:09:16.477

6x^-1 means multiplicative inverse of x, i.e., the number y such that xy = 1. In the field of real numbers, 2^-1 = 0.5. In the ring of integers modulo 7, 2^-1 = 4. – Dennis – 2015-12-29T16:15:33.547

4Modular arithmetic is weird. – SuperJedi224 – 2015-12-29T16:28:10.653

3@SuperJedi224 Modular arithmetic is weird, and yet you probably do it at least once a day without realizing it. If you use 12 hour time, and someone asks you to call them in two hours, and it's 11:00 and you decide to call them at 1:00, you just did modular arithmetic. I find it neat that one of the numbers on this clock is expressed in a way that is sometimes called "clock arithmetic". – Todd Wilcox – 2015-12-29T18:17:32.190

"Regarding the multiple possible values for x, 11 is equivalent to 4 modulo 7, so they're really the same". Ugh, so it's a 5 hour clock. :P – Otheus – 2015-12-30T07:33:35.647

Answers

17

Jelly, 6 bytes

R2*%i1

Try it online!

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.

Dennis

Posted 2015-12-28T23:00:56.100

Reputation: 196 637

13

Pyth - 9 8 bytes

f!t.^2TQ

Test Suite.

filters from default of 1 till it finds some x such that modular exponentiation with 2 and the input equals 1.

Maltysen

Posted 2015-12-28T23:00:56.100

Reputation: 25 023

11

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.

xnor

Posted 2015-12-28T23:00:56.100

Reputation: 115 687

8

Mathematica, 24 bytes

2~MultiplicativeOrder~#&

I just used a built-in for this.

LegionMammal978

Posted 2015-12-28T23:00:56.100

Reputation: 15 731

20Of course Mathematica has a built-in for this. :P – El'endia Starman – 2015-12-29T01:13:27.327

7@El'endiaStarman Of course Mathematica has an ungolfable built-in for this. :-{D – wizzwizz4 – 2015-12-29T18:08:01.050

7

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.

Alex A.

Posted 2015-12-28T23:00:56.100

Reputation: 23 761

1Also 8: ⍴∘∪⊢|2*⍳. – lirtosiast – 2015-12-30T02:41:03.067

6

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

Adnan

Posted 2015-12-28T23:00:56.100

Reputation: 41 965

I get 66\n132\n198 for an input of 201. – El'endia Starman – 2015-12-28T23:17:35.320

@El'endiaStarman sorry, wrong link :p – Adnan – 2015-12-28T23:18:19.273

Oh, haha, it's good now. :) – El'endia Starman – 2015-12-28T23:19:06.173

5

JavaScript (ES6), 28 bytes

f=(n,t=2)=>t<2||-~f(n,2*t%n)

Based on @xnor's brilliant recursive approach.

ETHproductions

Posted 2015-12-28T23:00:56.100

Reputation: 47 880

Do you have a link I can test this at? Doesn't seem to work in the console on Chrome. (SyntaxError due to =>, I think.) – El'endia Starman – 2015-12-28T23:21:38.237

@El'endiaStarman Here ya go..

– Conor O'Brien – 2015-12-28T23:43:22.603

@CᴏɴᴏʀO'Bʀɪᴇɴ: I can't figure out how to test it. – El'endia Starman – 2015-12-29T00:21:32.663

@El'endiaStarman This code defined a function which can be called like f(3). For some stupid reason, that website won't let you use this function unless you declare it with let or var. Try this.

– ETHproductions – 2015-12-29T00:26:04.550

Oh, okay. Thanks! Looks good. – El'endia Starman – 2015-12-29T00:39:19.020

Additionally, it would be nice to see a full test suite passed at a glance, e.g.: https://jsbin.com/jebetevume/1/edit?js,console

– Pavlo – 2015-12-29T12:21:58.047

1@Pavlo I know lambdas are accepted, but this function needs to be named so it can call itself. I'll add a test suite link when I get back to my computer. – ETHproductions – 2015-12-29T13:31:28.667

5

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

Adnan

Posted 2015-12-28T23:00:56.100

Reputation: 41 965

5

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.

Glen O

Posted 2015-12-28T23:00:56.100

Reputation: 2 548

5

Seriously, 14 bytes

1,;╗R`╙╜@%`Míu

Hex Dump:

312c3bbb5260d3bd4025604da175

Try It Online

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.

quintopia

Posted 2015-12-28T23:00:56.100

Reputation: 3 899

4

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.

xnor

Posted 2015-12-28T23:00:56.100

Reputation: 115 687

How might I test this? – El'endia Starman – 2015-12-29T01:12:33.967

See here

– Lynn – 2015-12-29T01:38:38.267

@Mauris: Thanks! – El'endia Starman – 2015-12-29T01:50:35.060

2

Japt, 17 bytes

1oU f@2pX %U¥1} g

Try it online!

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

ETHproductions

Posted 2015-12-28T23:00:56.100

Reputation: 47 880

2

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!

Alex A.

Posted 2015-12-28T23:00:56.100

Reputation: 23 761

No need for the map command, just use 2.^(1:n)%n. – Glen O – 2015-12-29T04:59:56.370

@GlenO That works perfectly, thanks! – Alex A. – 2015-12-29T05:03:59.330

2

PARI/GP, 20 bytes

n->znorder(Mod(2,n))

alephalpha

Posted 2015-12-28T23:00:56.100

Reputation: 23 988

2

Perl 5, 29 bytes

$n=<>;{2**++$_%$n-1?redo:say}

Hat tip.

msh210

Posted 2015-12-28T23:00:56.100

Reputation: 3 094

2

MATL, 13 bytes

it:Hw^w\1=f1)

Runs on Octave with the current GitHub commit 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

Luis Mendo

Posted 2015-12-28T23:00:56.100

Reputation: 87 464

2

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

Uses a custom encoding.

This answer is non-competing because it uses a version of Unicorn made after this language

Downgoat

Posted 2015-12-28T23:00:56.100

Reputation: 27 116

3The rainbows and unicorns are strong with this one... – Mama Fun Roll – 2015-12-30T02:21:59.587

Somebody come up with UnicornRLE – Sebi – 2015-12-31T15:36:59.473

Am I the only one getting ((2)2(2))(()) out of the code with @Downgoat's interpreter? – Rɪᴋᴇʀ – 2016-01-18T23:13:07.620

2

, 11 chars / 22 bytes

↻2ⁿḁ%ï>1)⧺ḁ

Try it here (Firefox only).

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

Mama Fun Roll

Posted 2015-12-28T23:00:56.100

Reputation: 7 234

1

CJam, 15 bytes

2qi,:)f#_,f%1#)

Peter Taylor saved a byte. Neat!

Lynn

Posted 2015-12-28T23:00:56.100

Reputation: 55 648

Rather than 1fe| you could :) and then ) after doing the #. – Peter Taylor – 2015-12-30T10:22:22.090

2qi,:)f#_,f%1#) – Peter Taylor – 2015-12-30T12:08:03.110

Ohh, of course. Thank you. – Lynn – 2015-12-30T12:19:58.423

0

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

Emigna

Posted 2015-12-28T23:00:56.100

Reputation: 50 798