An elegant way to define a sequence I am trying to define a sequence.
The first few terms of the sequence are:
$2,5,13,43,61$
Not yet found other terms because I am working with paper and pen, no software.
Why the first term is $5$?
Let be $\pi(x)$ the celebrated prime counting function.
Well 5-$\pi(5)$=$5-3$=2 which is a prime. 
If we repeat the same thing with the new prime $2$, we have 2-$\pi(2)=1$, which is not a prime. So starting the sequence from prime $5$, we have the cycle $5\rightarrow 2\rightarrow 1$. The arrows stop when a not prime is reached. No prime below $5$ has a longer cycle. Infact starting for example from $3$ you get $3-\pi(3)=1$, which is not prime so the cycle is simply $3\rightarrow 1$. 
The second term of the sequence is $13$ because below $13$ no other prime has a larger cycle. Infact $13-\pi(13)=7$, which is prime.  Then $7-\pi(7)=3$, which is prime and eventually $3-\pi(3)=1$, which is not prime. So the cycle is $13\rightarrow 7\rightarrow 3\rightarrow 1$
The cycle for 43 is longer so it is the third term of the above sequence.
Could you suggest to me a nice and elegant definition for this sequence: $5,13,43,61...$ (I don't know if it is infinite)
Could you find other terms with Pari if you want?
 A: I believe your sequence continues forever but grows quickly.  If $n$ is large, the density of primes around $n$ is $\log n$.  Since $\log n$ is so much smaller than $n$, the chance a random $n$ has $k$ arrows is about $\frac 1{(\log n)^{k+1}}$.  The expected number of sequences of length $k$ above $10^{12},$ say, is then $\int_{10^{12}}^\infty \frac {dn}{(\log n)^{k+1}}$.  This diverges because $(\log n)^k$ becomes less than $n$ for $n$ large enough and we know the integral of $\frac 1n$ diverges.  Each subtraction is only of order $\frac n{\log n}$, which is small compared to $n$ and the log will not change much.  
If we ask what length of sequence we expect to find among the $12$ digit numbers, we note that the log of these numbers is about $29$ and that $29^{8.5} \approx 3\cdot 10^{12}$.  We would expect to find some sequences of $7$ arrows, maybe $8$ or $9$, and be surprised at $10$ or more.  For $100$ digit numbers, the log is about $231$ and $231^{42.5} \approx 3\cdot 10^{100}$, so we would expect some sequences of length $40$ or $41$ among the $100$ digit numbers.
A: `my(a=0,b=0);forprime(x=1,50000,y=x;while(isprime(y-primepi(y)),y-=primepi(y);b++);if(b>a,a=b;print(x));b=0)`

produces 14897 as the next one. Then no more below 500000. There's not too much to say except primes in the sequence will be primes at even indices after the first, simply because most primes are more than 2 away from their indices. 
A: Using $S$ to denote the sequence you're trying to define, one may do so in 
terms of two auxiliary functions $N$ and $L$, where $N$ assigns to every prime number $x$ a sequence whose first term, denoted by $(N(x))(0)$$^*$, is $x$ itself, and each next term, denoted by $(N(x))(n + 1)$, is given by $(N(x))(n) - \pi((N(x))(n))$, and $L$ is the function which gives the number of terms of a sequence returned by $N$ up to when the first non-prime term is reached. $S$ is then defined to be such that the first term equals $5$, and given any term $S(n)$, the next term in the sequence is then the smallest prime number $p$ such that $L(N(p)) > L(N(S(n)))$.
In formula:
$S(0) = 5$
$S(n + 1) = \langle\downarrow p : p \in \mathbb{P} : L(N(p)) > L(N(S(n)))\rangle$
$(N(x))(0) = x$
$(N(x))(n + 1) = (N(x))(n) - \pi((N(x))(n))$
$L(N(x)) = \langle\downarrow n : n \in \mathbb{N} : (N(x))(n) \notin 
\mathbb{P}\rangle$
The notation $\langle\downarrow x : R(x) : T(x)\rangle$ here denotes the minimum element $x$ that satisfies $T(x)$ from the set of all elements satisfying $R(x)$. $R(x)$ and $T(x)$ denote arbitrary predicates (i. e. boolean-valued functions) which generally depend on $x$.

$^*$Note: Here we use the definition that a sequence is any function whose domain consists of either all natural numbers or all natural numbers $n$ such that $0 \le n \lt m$ for arbitrary natural constant $m$. We admit $0$ as the smallest natural number.
