Walking on the Hypercube
Jelly, 9 bytes
%⁴2*^µÐḶL
Takes two command-line arguments.
%⁴2*^µÐḶL A monadic link. Inputs: a_0. b also taken from command line.
%⁴2*^ Variadic link. Input: a
%⁴ a modulo b. ⁴ is second input, b.
2* Get 2 to that power
^ and bitwise xor with a.
µ Start a new, monadic link (input: a_0)
ÐḶ All elements of the cycle created when the preceding link
is applied repeatedly, starting with a_0.
L Length.
Try it here.
Haskell, 124
import Data.Bits
(y:z:w)%(x:s)|x==y||x==z=[i|(i,r)<-zip[1..]s,r==x]!!0|0<1=w%s
g n=(tail>>=(%)).iterate(\a->xor a$2^mod a n)
This finds the circle by the two-pointers-going-around-in-different-speeds algorithm, and heavily uses/abuses Haskell's approach to lists (for example, the two pointers are actually lists).
g
is the function that computes the answer. give it n
and then a[0]
and it will return the number to you (note that n
should be defined to be of type Int
to avoid type ambiguity).