Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fibonacci, lucas, and catalan sequences #655

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions book/src/list-functions-math.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,60 @@ fn binom(n: Scalar, k: Scalar) -> Scalar

</details>

### `fibonacci` (Fibonacci numbers)
The nth Fibonacci number, where n is a nonnegative integer. The Fibonacci sequence is given by \\( F_0=0 \\), \\( F_1=1 \\), and \\( F_n=F_{n-1}+F_{n-2} \\) for \\( n≥2 \\). The first several elements, starting with \\( n=0 \\), are \\( 0, 1, 1, 2, 3, 5, 8, 13 \\).
More information [here](https://en.wikipedia.org/wiki/Fibonacci_sequence).

```nbt
fn fibonacci(n: Scalar) -> Scalar
```

<details>
<summary>Examples</summary>

<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code" onclick=" window.open('https://numbat.dev/?q=fibonacci%285%29')""></button></div><code class="language-nbt hljs numbat">fibonacci(5)

= 5
</code></pre>

</details>

### `lucas` (Lucas numbers)
The nth Lucas number, where n is a nonnegative integer. The Lucas sequence is given by \\( L_0=2 \\), \\( L_1=1 \\), and \\( L_n=L_{n-1}+L_{n-2} \\) for \\( n≥2 \\). The first several elements, starting with \\( n=0 \\), are \\( 2, 1, 3, 4, 7, 11, 18, 29 \\).
More information [here](https://en.wikipedia.org/wiki/Lucas_number).

```nbt
fn lucas(n: Scalar) -> Scalar
```

<details>
<summary>Examples</summary>

<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code" onclick=" window.open('https://numbat.dev/?q=lucas%285%29')""></button></div><code class="language-nbt hljs numbat">lucas(5)

= 11
</code></pre>

</details>

### `catalan` (Catalan numbers)
The nth Catalan number, where n is a nonnegative integer. The Catalan sequence is given by \\( C_n=\frac{1}{n+1}\binom{2n}{n}=\binom{2n}{n}-\binom{2n}{n+1} \\). The first several elements, starting with \\( n=0 \\), are \\( 1, 1, 2, 5, 14, 42, 132, 429 \\).
More information [here](https://en.wikipedia.org/wiki/Catalan_number).

```nbt
fn catalan(n: Scalar) -> Scalar
```

<details>
<summary>Examples</summary>

<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code" onclick=" window.open('https://numbat.dev/?q=catalan%285%29')""></button></div><code class="language-nbt hljs numbat">catalan(5)

= 42
</code></pre>

</details>

## Random sampling, distributions

Defined in: `core::random`, `math::distributions`
Expand Down
13 changes: 13 additions & 0 deletions examples/tests/math_functions.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ assert_eq(binom(1.5, 2), 0.375)
assert_eq(binom(1.5, 3), -0.0625)
assert_eq(binom(1.5, 4), 0.0234375)

# combinatoric sequences
assert_eq(fibonacci(0), 0)
assert_eq(fibonacci(1), 1)
assert_eq(fibonacci(5), 5)

assert_eq(lucas(0), 2)
assert_eq(lucas(1), 1)
assert_eq(lucas(5), 11)

assert_eq(catalan(0), 1)
assert_eq(catalan(1), 1)
assert_eq(catalan(5), 42)

# maximum

assert_eq(maximum([1]), 1)
Expand Down
34 changes: 34 additions & 0 deletions numbat/modules/math/combinatorics.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,37 @@ fn binom(n: Scalar, k: Scalar) -> Scalar =
0
else
falling_factorial(n, k) / k!

@name("Fibonacci numbers")
@description("The nth Fibonacci number, where n is a nonnegative integer. The Fibonacci sequence is given by $F_0=0$, $F_1=1$, and $F_n=F_\{n-1\}+F_\{n-2\}$ for $n≥2$. The first several elements, starting with $n=0$, are $0, 1, 1, 2, 3, 5, 8, 13$.")
@url("https://en.wikipedia.org/wiki/Fibonacci_sequence")
@example("fibonacci(5)")
fn fibonacci(n: Scalar) -> Scalar =
if !(is_integer(n) && n >= 0) then
error("the argument to fibonacci(n) must be a nonnegative integer")
else
# use Binet's formula for constant time
round((phi^n - (-phi)^(-n))/sqrt(5))
where phi = (1+sqrt(5))/2

@name("Lucas numbers")
@description("The nth Lucas number, where n is a nonnegative integer. The Lucas sequence is given by $L_0=2$, $L_1=1$, and $L_n=L_\{n-1\}+L_\{n-2\}$ for $n≥2$. The first several elements, starting with $n=0$, are $2, 1, 3, 4, 7, 11, 18, 29$.")
@url("https://en.wikipedia.org/wiki/Lucas_number")
@example("lucas(5)")
fn lucas(n: Scalar) -> Scalar =
if !(is_integer(n) && n >= 0) then
error("the argument to lucas(n) must be a nonnegative integer")
else
# use Binet's formula for constant time
round(phi^n + (1-phi)^n)
where phi = (1+sqrt(5))/2

@name("Catalan numbers")
@description("The nth Catalan number, where n is a nonnegative integer. The Catalan sequence is given by $C_n=\frac\{1\}\{n+1\}\binom\{2n\}\{n\}=\binom\{2n\}\{n\}-\binom\{2n\}\{n+1\}$. The first several elements, starting with $n=0$, are $1, 1, 2, 5, 14, 42, 132, 429$.")
@url("https://en.wikipedia.org/wiki/Catalan_number")
@example("catalan(5)")
fn catalan(n: Scalar) -> Scalar =
if !(is_integer(n) && n >= 0) then
error("the argument to catalan(n) must be a nonnegative integer")
else
binom(2*n, n) / (n+1)
Loading