Thanks to @Dennis for -2 bytes!
f=lambda n,i=1:n and-~f(n-(~-7**i%i**2<1),i+1)
A one-indexed recursive function that takes input via argument and returns the result.
Try it online! (Recursion limit increased to allow the final test case to run)
How it works
n
is the desired index, and i
is the counting variable.
The expression ~-7**i%i**2<1
returns True
(equivalent to 1
) if i^2
divides 7^i - 1
, and False
(equivalent to 0
) otherwise. Each time the function is called, the result of the expression is subtracted from n
, decrementing n
each time a hit is found; i
is also incremented.
The short-circuiting behaviour of and
means that when n
is 0
, 0
is returned; this is the base case. Once this is reached, recursion stops, and the current value of i
is returned by the original function call. Rather than explicitly using i
, this is done using the fact that for each function call, an increment has been performed using the -~
in front of the call; incrementing 0
i
times gives i
, as required.
n
? I can give the correct result withn=9
, butn=10
is already causing me problems. \$\endgroup\$n=10
gives me 32; it's because it starts using double instead of integers and the mod is wrong after that. :( \$\endgroup\$