As your needs get more elaborate, it is useful to factor your problem into simpler subproblems. You may then combine the factors to get your result. So, your elementary calculation appears to be:
seq[i_, j_, k_] := Table[Mod[i*2^n + j*4^n + k*6^n, 7], {n, 0, 5}]
Try it:
seq[0, 0, 1]
yields:
{1, 6, 1, 6, 1, 6}
There are various ways to approach your filter problem. You can generate the whole list, and filter with something like Select
, or you can check "on the fly". The latter is more fun here, so:
filtseq[i_, j_, k_, test_] :=
With[{s = seq[i, j, k]}, If[test[s], {s, {i, j, k}}, Nothing]]
This takes your indices, along with a test function. If the test, applied to the sequence, yields True
, it yields the sequence packaged with its indices. If not, it yields Nothing
. Mathematica is not a procedural programming language. It is an expression rewriting system. In particular, the rewrite rules bound to Nothing
cause it to disappear without a trace from lists. Here, the With
allows me to avoid evaluating the sequence twice by evaluating it once and giving the result a local name, s
.
The next step is to write your test:
testprod[seq_] := Times @@ seq == 216 || Times @@ seq == 1000
Times@@
is an idiom for multiplying the elements of a list. Look at its FullForm
some time. A With
would be good here, too, but I'm lazy.
Always try out your definitions. Keep them simple. Don't expect to construct a complicated program and have it work the first time, or to produce comprehensible output if it doesn't.
testprod[{1, 6, 1, 6, 1, 6}]
Yields True
. Other tests omitted here. Next, try the higher level:
filtseq[0, 0, 1, testprod]
{{1, 6, 1, 6, 1, 6}, {0, 0, 1}}
filtseq[1, 0, 1, testprod]
Nothing
I don't entirely understand what you want at higher level, but at least we have some basic building blocks. Here's a function that keeps your original definition of the sequences, but lets you change the filter criterion:
seqlist[test_] :=
Flatten[Table[filtseq[i, j, k, test], {i, 0, 5}, {j, 0, 5}, {k, 0, 5}], 2]
The Table
makes a deeply nested list. The Flatten
here takes away the top two layers of nesting.
seqlist[testprod]
{{{1, 6, 1, 6, 1, 6}, {0, 0, 1}}, {{2, 5, 2, 5, 2, 5}, {0, 0, 2}}, {{5, 2, 5, 2, 5, 2}, {0, 0, 5}}}
Note that the test need not be a named function. An anonymous Function
expression works fine:
seqlist[Times @@ # == 64 &]
{{{1, 4, 2, 1, 4, 2}, {0, 1, 0}}, {{2, 1, 4, 2, 1, 4}, {0, 2, 0}},
{{4, 2, 1, 4, 2, 1}, {0, 4, 0}}, {{1, 2, 4, 1, 2, 4}, {1, 0, 0}},
{{2, 4, 1, 2, 4, 1}, {2, 0, 0}}, {{4, 1, 2, 4, 1, 2}, {4, 0, 0}}}