# Executable library

Library saved as *Hailstone.sm*

```ruby
func hailstone(n) {
    gather {
        while (n > 1) {
            take(n)
            n = (n.is_even ? (n/2) : (take(3*n + 1)/2))
        }
        take(1)
    }
}

if (__FILE__ == __MAIN__) {             # true when not imported
    var seq = hailstone(27)
    say "hailstone(27) - #{seq.len} elements: #{seq.first(4)} [...] #{seq.last(4)}"
 
    var n = 0
    var max = 0
    100_000.times { |i|
        var seq = hailstone(i)
        if (seq.len > max) {
            max = seq.len
            n = i
        }
    }
 
    say "Longest sequence is for #{n}: #{max}"
}
```

It can be run with:

```
$ sidef Hailstone.sm
```

It can then be used with a program such as:

```ruby
include Hailstone
 
var score = Hash()
100_000.times { |i| score{ Hailstone::hailstone(i).len } := 0 ++ }
 
var k = score.keys.max_by {|k| score{k} }
say "Most common length is #{k}, occurring #{score{k}} times"
```

Called with a command line as:

```
$ sidef test_hailstone.sf
```

The library is searched in the directories specified in the *SIDEF\_INC* environment variable, which defaults to the current directory.


---

# 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/e/executable_library.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.
