is there any non prime (pseudoprime) that holds true for this test, or any prime that fall out of this test? The sequence is defined by the following formula   $d_{n + 3} = d_{n + 2} + 2d_{n + 1} - d_n$ where $d_1 = 0, d_2 = 1, d_3 = 2$, $\{0, 1, 2, 4, 7, 13, 23,42, ...\}$ if this sequence is calculated over finite field $p$ $$(d_{n + 3} = d_{n + 2} + 2d_{n + 1} - d_n)\,mod\,\text{p}$$ It follows that if $p$ is prime, then the last three terms $d_{p - 1},\; d_p$, and  $d_{p + 1}$ fall within one of the following ending patterns
$\;\{4, 2, 1\}$ , $\{p - 1, 0, 1\}$, or  $\{p - 6, p - 3, p - 2\}$. this is true for all prime up to tested range, $60000$

my questions



*

*why only primes follow these patterns?

*is there any non prime(s) following this patterns?

*is there any published work like this and in the same pattern. To see if i can study more about it

 A: Prime $p=7$ fails the test, but this is the only exception below $10^7$. This is likely explained by the characteristic polynomial $x^3 - x^2 - 2x + 1$ having discriminant $49=7^2$.
Also, there are nine pseudoprimes below $10^7$: 
$$530881,~ 597871,~ 1152271,~ 3057601,~ 3581761,~ 3698241,~ 5444489,~ 5968873,~ 6868261.$$
A: This should really be just a comment, but I had some technical difficulties.
Consider a number $x$ whose consecutive powers satisfy the recursive formula in the definition of your sequence, that is,
$$x^3=x^2+2x-1.\qquad{(1)}
$$
This equation has three different roots, all of them real. Call them $x_1,x_2,x_3$. Let $a_1,a_2,a_3\in\mathbb{R}$ be arbitrary. For $n\in\mathbb{N}$, define
$$c_n=a_{1}x_{1}^n+a_{2}x_{2}^n+a_{3}x_{3}^n.\qquad{(2)}
$$
Now $c_{n+3}=c_{n+2}+2c_{n+1}-c_{n}$ for all $n$.
On the other hand, given any starting values for $c_1,c_2,c_3$, you can solve for $a_1,a_2,a_3$. So, in particular, there are values for the coefficients $a_1,a_2,a_3$ which give the sequence $(d_n)_{n\in\mathbb{N}}$ by the formula $(2)$.
I suggest having a look at equation $(1)$ modulo a number. I'm afraid I haven't yet developed this idea further, so please regard this as a rather long comment.
