A083569: Smallest m not occurring earlier such that m+n is prime Define a 1-indexed sequence as follows:


A083569(1) = 1
A083569(n) where n is an integer greater than 1, is the smallest integer m not occurring earlier such that m+n is a prime number.


Your task is to take in n and return A083569(n).

 n  A083569(n)
 1  1
 2  3
 3  2
 4  7
 5  6
 6  5
 7  4
 8  9
 9  8
10 13
11 12
12 11
13 10
14 15
15 14
16 21
17 20
18 19
19 18
20 17


More testcases can be found here. The original sequence on OEIS can be found here.

This is code-golf. Shortest answer in bytes wins. Standard loopholes apply.
Haskell, 87 86 83 80 74 69 bytes

Thanks to xnor for suggesting some changes that saved 3 bytes!

f n=[m|m&lt;-[1..],all((&gt;0).mod(n+m))[2..n+m-1],all((/=m).f)[1..n-1]]!!0


Try it online!

I'm new to Haskell, and Haskell golfing, feedback is appreciated!

Explanation

We define a function f n.  We define f n to be the first element !!0 of the list:

[m|m&lt;-[1..],all((&gt;0).mod(n+m))[2..n+m-1],all((/=m).f)[1..n-1]]


Broken down that is:

[m|          # Numbers m
m&lt;-[1..],    # From the integers greater than 0
all          # Forall x
(&gt;0).mod(n+m)# n+m mod x is not zero
[2..n+m-1]   # from the integers from 2 to n+m-1
all          # Forall
((/=m).f)    # when f is applied the result is not m
[1..n-1]     # from the integers from 1 to n-1