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.

Thanks in advance.

  • 2
    $\begingroup$ 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. $\endgroup$ – b3m2a1 Jul 12 '19 at 6:53
  • 2
    $\begingroup$ you can use Complement only once: Complement[Range[20], Flatten[Table[m+2n + (-1)^n, {n, 4}, {m, 2}], 1], Flatten[Table[m - 2n + (-1)^n, {n, 4}, {m, 2}], 1]] $\endgroup$ – kglr Jul 12 '19 at 7:07
  • 3
    $\begingroup$ @kglr my guess, though, is there is some easy analytical-type solution to this thing once we know what the intended operation of the function should be... $\endgroup$ – b3m2a1 Jul 12 '19 at 7:08
  • 2
    $\begingroup$ BTW, Hi and welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence. Thanks for your minimal working example of code in properly formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Jul 12 '19 at 10:03
  • 1
    $\begingroup$ 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. $\endgroup$ – Michael E2 Jul 13 '19 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.