Generating a sequence | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Generating a sequence

I'm doing an Code on Cayley's Formula (a labelled Tree with n nodes has n^(n-2) possibilities) I'm using the Prüfer-seq proof in which you show that a sequence that has definitely n^(n-2) possibilities is convertible to a tree and backwards. I have a working code, but generating the sequence is very inefficient, in my code it works by creating an array of 1's and adding 1 to the last index, if that index has a too high value it resets to 1 and the index left of it gets increased. Is there a better way to?

7th Nov 2019, 2:18 PM
Anton Böhler
Anton Böhler - avatar
2 Answers
+ 1
if someone ever searches for this problem I have this solution: function sequence(nodes, index){ var seq = []; for(var i=0;i<nodes-2;i++){ seq.push(index % nodes + 1); index = Math.floor(index / nodes); } seq.reverse(); return seq; }
7th Nov 2019, 4:29 PM
Anton Böhler
Anton Böhler - avatar
0
i have a range input which is like a 'index' to which sequence I want. Every Frame I calculate this sequence so yeah a faster way would be great ...
7th Nov 2019, 2:36 PM
Anton Böhler
Anton Böhler - avatar