# Fusc sequence

```ruby
func fusc(n) is cached {

    return 0 if n.is_zero
    return 1 if n.is_one

    n.is_even ? fusc(n/2) : (fusc((n-1)/2) + fusc(((n-1)/2)+1))
}

say ("First 61 terms of the Stern-Brocot sequence: ", 61.of(fusc).join(' '))

say "\nIndex and value for first term longer than any previous:"
printf("%15s : %s\n", "Index", "Value");

var (index=0, len=0)

5.times {
    index = (index..Inf -> first_by { fusc(_).len > len })
    len = fusc(index).len
    printf("%15s : %s\n", index.commify, fusc(index).commify)
}
```

## Output:

```
First 61 terms of the Stern-Brocot sequence: 0 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 1 5 4 7 3 8 5 7 2 7 5 8 3 7 4 5 1 6 5 9 4 11 7 10 3 11 8 13 5 12 7 9 2 9 7 12 5 13 8 11 3 10 7 11 4

Index and value for first term longer than any previous:
          Index : Value
              0 : 0
             37 : 11
          1,173 : 108
         35,499 : 1,076
        699,051 : 10,946
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/f/fusc_sequence.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
