# Efficient Mathematica Code for List Problem

Say we are given $$f(m,n)=m \pm 2n + (-1)^n$$ where $$m>0, n>0$$, $$m$$ as well as $$n$$ are integers. The problem is to generate a list of all the integers that will not be found in the list of values of the function $$f$$ for a particular $$m$$ and $$n$$.

I came up with the code for the 'plus' sign as

a = Complement[Table[i,{i,1,20}], Flatten[Table[m+2n + (-1)^n, {n, 4}, {m, 2}], 1]]


and the 'minus'sign as

b = Complement[Table[i,{i,1,20}], Flatten[Table[m-2n + (-1)^n, {n, 4}, {m, 2}], 1]]


After which I now executed

Intersection[a, b]


This gave me the result i wanted though it is highly inefficient because as $$m$$, $$n$$ increases, the codes take much time. I need $$m$$, $$n$$ up to about $$10^9$$.

I want an efficient code that will be executed once.

• To be honest, I don't quite understand your explanation of what you want to do. You want the complement of the range of $f$ for a given maximum $m$ and $n$? Could you add some more detail. As it stands, I don't see how your code is supposed to solve your problem. – b3m2a1 Jul 12 '19 at 6:53
• you can use Complement only once: Complement[Range, Flatten[Table[m+2n + (-1)^n, {n, 4}, {m, 2}], 1], Flatten[Table[m - 2n + (-1)^n, {n, 4}, {m, 2}], 1]] – kglr Jul 12 '19 at 7:07
• It seems to me that as soon as the m iterator is as big as {m, 4}, you get a run of consecutive integers. If {m, 3}, the complement consists of all the integers (in the range of the table) that are congruent to 1 mod 4. And so on. – Michael E2 Jul 13 '19 at 3:12