Sumac Sequence Calculator

I randomly stumbled upon this Sumac Sequence challenge, and while I wasn’t interested in coming up with the most compact solution, it was something that I wanted to build a readable solution and tutorial around!

See it Live
You can see my example here:

Play with it here:

What is a Sumac Sequence?
There are a bunch of mathematical ways of describing it. The easiest way to think of it is as follows. You start out with two numbers, like 100 and 40. The third number is the difference of the previous two numbers. This means the third number is 100 - 40 = 60. The sequence right now is 100, 40, and 60.

Is there a fourth number? Well…the previous two numbers are now 40 and 60, and the difference between them is -20. As it turns out, because this result is negative, the sequence terminates. The numbers in our sumac sequence have to be greater than 0.

This means the sumac sequence for our example is just 100, 40, and 60. You can see how this plays out for a variety of other numbers. Try 120 and 71 for a sequence of 5 numbers. Can you come up with two numbers that result in more than 5 numbers getting generated in the sequence? :crazy_face:

The full code is in the example source, but ty snippet for calculating the Sumac sequence is as follows:

function getTerms(a, b) {
  if (b > 0) {
    getTerms(b, a - b);
  } else {

I’m just keeping it simple and relying on recursion. I will write a tutorial on this in the near future, but I wanted to share this now in case I end up taking a ridiculous wrong time to get that wrapped up haha.