3
$\begingroup$

There is a sequence of numbers that satisfies
$a_1=2$, $a_n-a_{n-1}\in \{1,3,5\}$
enter image description here
I tried to enumerate all possibilities using the code below

n = 3;
NestList[Join @@ Outer[Plus, #, {1, 3, 5}] &, {{2}}, n - 1]

{{{2}}, {{3, 5, 7}}, {{4, 6, 8}, {6, 8, 10}, {8, 10, 12}}}

Now I hope to get such a list, is there an easy way?

{{2,3,4},{2,3,6},{2,3,8},{2,5,6},{2,5,8},{2,5,10},{2,7,8},{2,7,10},{2,7,12}}

$\endgroup$

2 Answers 2

5
$\begingroup$
Accumulate /@ Prepend[2] /@ Tuples[{1, 3, 5}, 2]
{{2, 3, 4}, {2, 3, 6}, {2, 3, 8}, {2, 5, 6}, {2, 5, 8}, {2, 5, 10}, > 
  {2, 7, 8}, {2, 7, 10}, {2, 7, 12}}

And, for fun:

ng = NestGraph[{1, 3, 5} + # &, 2, 2, VertexLabels -> Automatic]

enter image description here

root = 2;
sinks = VertexList[ng, _?(VertexOutDegree[ng, #] == 0 &)];

Join @@ (FindPath[ng, ##, Infinity, All] & @@@ Thread[{root, sinks}])
{{2, 3, 4}, {2, 5, 6}, {2, 3, 6}, {2, 7, 8}, {2, 5, 8}, {2, 3, 8}, 
 {2, 7, 10}, {2, 5, 10}, {2, 7, 12}}
$\endgroup$
2
  • $\begingroup$ Can we generate such a graph? i.sstatic.net/a8mKd.png NestGraph[{1,3,5}+#&,2,3,VertexLabels->Automatic] It doesn't work that way. $\endgroup$
    – expression
    Commented Jan 3, 2022 at 10:17
  • $\begingroup$ @expression, I can't think of a simple way to get the graph in the link. You might want to post it as a new question with a brief explanation of the context. $\endgroup$
    – kglr
    Commented Jan 3, 2022 at 13:53
2
$\begingroup$
FoldList[Plus, 2, #] & /@ Tuples[{1, 3, 5}, 2]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.