# Sumac Sequence Calculator

**URL:** https://forum.kirupa.com/t/sumac-sequence-calculator/645957
**Category:** web dev
**Created:** [February 2, 2021, 6:05am UTC](https://forum.kirupa.com/t/sumac-sequence-calculator/645957 "2021-02-02T06:05:49Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [February 2, 2021, 6:05am UTC](https://forum.kirupa.com/t/sumac-sequence-calculator/645957/1 "2021-02-02T06:05:49Z")

</div>

I randomly stumbled upon this [Sumac Sequence challenge](https://codegolf.stackexchange.com/questions/148911/length-of-a-sumac-sequence), 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:

 ![Screen Shot 2021-02-01 at 10.24.24 PM](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/2X/0/0b9917e154a2e08e64b798ecde98abf6913b1e30.jpeg)

Play with it here: [Sumac Sequence Visualizer](https://www.kirupa.com/html5/examples/sumac_sequence.htm)

**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? 🤪

**Snippet**  
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) {
    sumac_sequence.push(a);
    getTerms(b, a - b);
  } else {
    sumac_sequence.push(a);
  }
}

```

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.

Cheers,  
Kirupa
