Pārlūkot izejas kodu

Added fibonacci, lucas, and catalan sequences

Robert Bennett 11 mēneši atpakaļ
vecāks
revīzija
82817116c0

+ 54 - 0
book/src/list-functions-math.md

@@ -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`

+ 13 - 0
examples/tests/math_functions.nbt

@@ -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)

+ 34 - 0
numbat/modules/math/combinatorics.nbt

@@ -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)