Bell numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 35: | Line 35: | ||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang=11l>F bellTriangle(n) |
<syntaxhighlight lang="11l">F bellTriangle(n) |
||
[[BigInt]] tri |
[[BigInt]] tri |
||
L(i) 0 .< n |
L(i) 0 .< n |
||
Line 91: | Line 91: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|GNAT|8.3.0}} |
{{works with|GNAT|8.3.0}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada"> |
||
with Ada.Text_IO; use Ada.Text_IO; |
with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Main is |
procedure Main is |
||
Line 188: | Line 188: | ||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
||
Uses Algol 68G's LONG LONG INT to calculate the numbers up to 50. Calculates the numbers using the triangle algorithm but without storing the triangle as a whole - each line of the triangle replaces the previous one. |
Uses Algol 68G's LONG LONG INT to calculate the numbers up to 50. Calculates the numbers using the triangle algorithm but without storing the triangle as a whole - each line of the triangle replaces the previous one. |
||
<syntaxhighlight lang=algol68>BEGIN # show some Bell numbers # |
<syntaxhighlight lang="algol68">BEGIN # show some Bell numbers # |
||
PROC show bell = ( INT n, LONG LONG INT bell number )VOID: |
PROC show bell = ( INT n, LONG LONG INT bell number )VOID: |
||
print( ( whole( n, -2 ), ": ", whole( bell number, 0 ), newline ) ); |
print( ( whole( n, -2 ), ": ", whole( bell number, 0 ), newline ) ); |
||
Line 231: | Line 231: | ||
=={{header|ALGOL-M}}== |
=={{header|ALGOL-M}}== |
||
<syntaxhighlight lang=algolm>begin |
<syntaxhighlight lang="algolm">begin |
||
integer function index(row, col); |
integer function index(row, col); |
||
integer row, col; |
integer row, col; |
||
Line 299: | Line 299: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
<syntaxhighlight lang=apl>bell←{ |
<syntaxhighlight lang="apl">bell←{ |
||
tr←↑(⊢,(⊂⊃∘⌽+0,+\)∘⊃∘⌽)⍣14⊢,⊂,1 |
tr←↑(⊢,(⊂⊃∘⌽+0,+\)∘⊃∘⌽)⍣14⊢,⊂,1 |
||
⎕←'First 15 Bell numbers:' |
⎕←'First 15 Bell numbers:' |
||
Line 323: | Line 323: | ||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=gwbasic> 100 LET ROWS = 15 |
<syntaxhighlight lang="gwbasic"> 100 LET ROWS = 15 |
||
110 LET M$ = CHR$ (13) |
110 LET M$ = CHR$ (13) |
||
120 LET N = ROWS: GOSUB 500"BELLTRIANGLE" |
120 LET N = ROWS: GOSUB 500"BELLTRIANGLE" |
||
Line 368: | Line 368: | ||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=rebol>bellTriangle: function[n][ |
<syntaxhighlight lang="rebol">bellTriangle: function[n][ |
||
tri: map 0..n-1 'x [ map 0..n 'y -> 0 ] |
tri: map 0..n-1 'x [ map 0..n 'y -> 0 ] |
||
set get tri 1 0 1 |
set get tri 1 0 1 |
||
Line 422: | Line 422: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="autohotkey">;----------------------------------- |
||
Bell_triangle(maxRows){ |
Bell_triangle(maxRows){ |
||
row := 1, col := 1, Arr := [] |
row := 1, col := 1, Arr := [] |
||
Line 454: | Line 454: | ||
} |
} |
||
;-----------------------------------</syntaxhighlight> |
;-----------------------------------</syntaxhighlight> |
||
Examples:<syntaxhighlight lang= |
Examples:<syntaxhighlight lang="autohotkey">MsgBox % Show_Bell_Number(Bell_triangle(15)) |
||
MsgBox % Show_Bell_triangle(Bell_triangle(10)) |
MsgBox % Show_Bell_triangle(Bell_triangle(10)) |
||
return</syntaxhighlight> |
return</syntaxhighlight> |
||
Line 487: | Line 487: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=c>#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 577: | Line 577: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=csharp>using System; |
<syntaxhighlight lang="csharp">using System; |
||
using System.Numerics; |
using System.Numerics; |
||
Line 666: | Line 666: | ||
{{libheader|Boost}} |
{{libheader|Boost}} |
||
Requires C++14 or later. If HAVE_BOOST is defined, we use the cpp_int class from Boost so we can display the 50th Bell number, as shown in the output section below. |
Requires C++14 or later. If HAVE_BOOST is defined, we use the cpp_int class from Boost so we can display the 50th Bell number, as shown in the output section below. |
||
<syntaxhighlight lang=cpp>#include <iostream> |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 753: | Line 753: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
<syntaxhighlight lang=clu>bell = cluster is make, get |
<syntaxhighlight lang="clu">bell = cluster is make, get |
||
rep = array[int] |
rep = array[int] |
||
Line 831: | Line 831: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
===via Bell triangle=== |
===via Bell triangle=== |
||
<syntaxhighlight lang=lisp>;; The triangle is a list of arrays; each array is a |
<syntaxhighlight lang="lisp">;; The triangle is a list of arrays; each array is a |
||
;; triangle's row; the last row is at the head of the list. |
;; triangle's row; the last row is at the head of the list. |
||
(defun grow-triangle (triangle) |
(defun grow-triangle (triangle) |
||
Line 930: | Line 930: | ||
===via Stirling numbers of the second kind=== |
===via Stirling numbers of the second kind=== |
||
This solution's algorithm is substantially slower than the algorithm based on the Bell triangle, because of the many nested loops. |
This solution's algorithm is substantially slower than the algorithm based on the Bell triangle, because of the many nested loops. |
||
<syntaxhighlight lang=lisp>;;; Compute bell numbers analytically |
<syntaxhighlight lang="lisp">;;; Compute bell numbers analytically |
||
;; Compute the factorial |
;; Compute the factorial |
||
Line 999: | Line 999: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=cowgol>include "cowgol.coh"; |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
typedef B is uint32; |
typedef B is uint32; |
||
Line 1,101: | Line 1,101: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<syntaxhighlight lang=d>import std.array : uninitializedArray; |
<syntaxhighlight lang="d">import std.array : uninitializedArray; |
||
import std.bigint; |
import std.bigint; |
||
import std.stdio : writeln, writefln; |
import std.stdio : writeln, writefln; |
||
Line 1,169: | Line 1,169: | ||
It shows a way of calculating Bell numbers without using a triangle. |
It shows a way of calculating Bell numbers without using a triangle. |
||
Output numbering is as in the statement of the task, namely B_0 = 1, B_1 = 1, B_2 = 2, .... |
Output numbering is as in the statement of the task, namely B_0 = 1, B_1 = 1, B_2 = 2, .... |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="delphi"> |
||
program BellNumbers; |
program BellNumbers; |
||
Line 1,237: | Line 1,237: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="elixir"> |
||
defmodule Bell do |
defmodule Bell do |
||
def triangle(), do: Stream.iterate([1], fn l -> bell_row l, [List.last l] end) |
def triangle(), do: Stream.iterate([1], fn l -> bell_row l, [List.last l] end) |
||
Line 1,279: | Line 1,279: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
===The function=== |
===The function=== |
||
<syntaxhighlight lang=fsharp> |
<syntaxhighlight lang="fsharp"> |
||
// Generate bell triangle. Nigel Galloway: July 6th., 2019 |
// Generate bell triangle. Nigel Galloway: July 6th., 2019 |
||
let bell=Seq.unfold(fun g->Some(g,List.scan(+) (List.last g) g))[1I] |
let bell=Seq.unfold(fun g->Some(g,List.scan(+) (List.last g) g))[1I] |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
===The Task=== |
===The Task=== |
||
<syntaxhighlight lang=fsharp> |
<syntaxhighlight lang="fsharp"> |
||
bell|>Seq.take 10|>Seq.iter(printfn "%A") |
bell|>Seq.take 10|>Seq.iter(printfn "%A") |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 1,300: | Line 1,300: | ||
[21147; 25287; 30304; 36401; 43833; 52922; 64077; 77821; 94828; 115975] |
[21147; 25287; 30304; 36401; 43833; 52922; 64077; 77821; 94828; 115975] |
||
</pre> |
</pre> |
||
<syntaxhighlight lang=fsharp> |
<syntaxhighlight lang="fsharp"> |
||
bell|>Seq.take 15|>Seq.iter(fun n->printf "%A " (List.head n));printfn "" |
bell|>Seq.take 15|>Seq.iter(fun n->printf "%A " (List.head n));printfn "" |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 1,307: | Line 1,307: | ||
1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322 |
1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322 |
||
</pre> |
</pre> |
||
<syntaxhighlight lang=fsharp> |
<syntaxhighlight lang="fsharp"> |
||
printfn "%A" (Seq.head (Seq.item 49 bell)) |
printfn "%A" (Seq.head (Seq.item 49 bell)) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 1,318: | Line 1,318: | ||
===via Aitken's array=== |
===via Aitken's array=== |
||
{{works with|Factor|0.98}} |
{{works with|Factor|0.98}} |
||
<syntaxhighlight lang=factor>USING: formatting io kernel math math.matrices sequences vectors ; |
<syntaxhighlight lang="factor">USING: formatting io kernel math math.matrices sequences vectors ; |
||
: next-row ( prev -- next ) |
: next-row ( prev -- next ) |
||
Line 1,353: | Line 1,353: | ||
This solution makes use of a [https://en.wikipedia.org/wiki/Bell_number#Summation_formulas recurrence relation] involving binomial coefficients. |
This solution makes use of a [https://en.wikipedia.org/wiki/Bell_number#Summation_formulas recurrence relation] involving binomial coefficients. |
||
{{works with|Factor|0.98}} |
{{works with|Factor|0.98}} |
||
<syntaxhighlight lang=factor>USING: formatting kernel math math.combinatorics sequences ; |
<syntaxhighlight lang="factor">USING: formatting kernel math math.combinatorics sequences ; |
||
: next-bell ( seq -- n ) |
: next-bell ( seq -- n ) |
||
Line 1,373: | Line 1,373: | ||
This solution defines Bell numbers in terms of [https://en.wikipedia.org/wiki/Bell_number#Summation_formulas sums of Stirling numbers of the second kind]. |
This solution defines Bell numbers in terms of [https://en.wikipedia.org/wiki/Bell_number#Summation_formulas sums of Stirling numbers of the second kind]. |
||
{{works with|Factor|0.99 development release 2019-07-10}} |
{{works with|Factor|0.99 development release 2019-07-10}} |
||
<syntaxhighlight lang=factor>USING: formatting kernel math math.extras math.ranges sequences ; |
<syntaxhighlight lang="factor">USING: formatting kernel math math.extras math.ranges sequences ; |
||
: bell ( m -- n ) |
: bell ( m -- n ) |
||
Line 1,384: | Line 1,384: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight lang=freebasic>#define MAX 21 |
<syntaxhighlight lang="freebasic">#define MAX 21 |
||
#macro ncp(n, p) |
#macro ncp(n, p) |
||
Line 1,408: | Line 1,408: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,481: | Line 1,481: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight lang=groovy>class Bell { |
<syntaxhighlight lang="groovy">class Bell { |
||
private static class BellTriangle { |
private static class BellTriangle { |
||
private List<Integer> arr |
private List<Integer> arr |
||
Line 1,569: | Line 1,569: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<syntaxhighlight lang=haskell>bellTri :: [[Integer]] |
<syntaxhighlight lang="haskell">bellTri :: [[Integer]] |
||
bellTri = |
bellTri = |
||
let f xs = (last xs, xs) |
let f xs = (last xs, xs) |
||
Line 1,621: | Line 1,621: | ||
And, of course, in terms of ''Control.Arrow'' or ''Control.Applicative'', the triangle function could also be written as: |
And, of course, in terms of ''Control.Arrow'' or ''Control.Applicative'', the triangle function could also be written as: |
||
<syntaxhighlight lang=haskell>import Control.Arrow |
<syntaxhighlight lang="haskell">import Control.Arrow |
||
bellTri :: [[Integer]] |
bellTri :: [[Integer]] |
||
Line 1,628: | Line 1,628: | ||
or: |
or: |
||
<syntaxhighlight lang=haskell>import Control.Applicative |
<syntaxhighlight lang="haskell">import Control.Applicative |
||
bellTri :: [[Integer]] |
bellTri :: [[Integer]] |
||
Line 1,634: | Line 1,634: | ||
or, as an applicative without the need for an import: |
or, as an applicative without the need for an import: |
||
<syntaxhighlight lang=haskell>bellTri :: [[Integer]] |
<syntaxhighlight lang="haskell">bellTri :: [[Integer]] |
||
bellTri = map snd (iterate (((,) . last <*> id) . uncurry (scanl (+))) (1, [1]))</syntaxhighlight> |
bellTri = map snd (iterate (((,) . last <*> id) . uncurry (scanl (+))) (1, [1]))</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang=text> |
<syntaxhighlight lang="text"> |
||
bell=: ([: +/\ (,~ {:))&.>@:{: |
bell=: ([: +/\ (,~ {:))&.>@:{: |
||
Line 1,663: | Line 1,663: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
<syntaxhighlight lang=java>import java.util.ArrayList; |
<syntaxhighlight lang="java">import java.util.ArrayList; |
||
import java.util.List; |
import java.util.List; |
||
Line 1,754: | Line 1,754: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{trans|Julia}} |
{{trans|Julia}} |
||
<syntaxhighlight lang=jq># nth Bell number |
<syntaxhighlight lang="jq"># nth Bell number |
||
def bell: |
def bell: |
||
. as $n |
. as $n |
||
Line 1,799: | Line 1,799: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Source: Combinatorics at https://github.com/JuliaMath/Combinatorics.jl/blob/master/src/numbers.jl |
Source: Combinatorics at https://github.com/JuliaMath/Combinatorics.jl/blob/master/src/numbers.jl |
||
<syntaxhighlight lang=julia>""" |
<syntaxhighlight lang="julia">""" |
||
bellnum(n) |
bellnum(n) |
||
Compute the ``n``th Bell number. |
Compute the ``n``th Bell number. |
||
Line 1,879: | Line 1,879: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=scala>class BellTriangle(n: Int) { |
<syntaxhighlight lang="scala">class BellTriangle(n: Int) { |
||
private val arr: Array<Int> |
private val arr: Array<Int> |
||
Line 1,963: | Line 1,963: | ||
In order to handle arrays, a program for the LMC has to modify its own code. This practice is usually frowned on nowadays, but was standard on very early real-life computers, such as EDSAC. |
In order to handle arrays, a program for the LMC has to modify its own code. This practice is usually frowned on nowadays, but was standard on very early real-life computers, such as EDSAC. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="little man computer"> |
||
// Little Man Computer, for Rosetta Code. |
// Little Man Computer, for Rosetta Code. |
||
// Calculate Bell numbers, using a 1-dimensional array and addition. |
// Calculate Bell numbers, using a 1-dimensional array and addition. |
||
Line 2,052: | Line 2,052: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<syntaxhighlight lang=lua>-- Bell numbers in Lua |
<syntaxhighlight lang="lua">-- Bell numbers in Lua |
||
-- db 6/11/2020 (to replace missing original) |
-- db 6/11/2020 (to replace missing original) |
||
Line 2,114: | Line 2,114: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang=maple>bell1:=proc(n) |
<syntaxhighlight lang="maple">bell1:=proc(n) |
||
option remember; |
option remember; |
||
add(binomial(n-1,k)*bell1(k),k=0..n-1) |
add(binomial(n-1,k)*bell1(k),k=0..n-1) |
||
Line 2,133: | Line 2,133: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
'''Function definition:''' |
'''Function definition:''' |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica"> |
||
BellTriangle[n_Integer?Positive] := NestList[Accumulate[# /. {a___, b_} :> {b, a, b}] &, {1}, n - 1]; |
BellTriangle[n_Integer?Positive] := NestList[Accumulate[# /. {a___, b_} :> {b, a, b}] &, {1}, n - 1]; |
||
BellNumber[n_Integer] := BellTriangle[n][[n, 1]]; |
BellNumber[n_Integer] := BellTriangle[n][[n, 1]]; |
||
Line 2,139: | Line 2,139: | ||
'''Output:''' |
'''Output:''' |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica"> |
||
In[51]:= Array[BellNumber, 25] |
In[51]:= Array[BellNumber, 25] |
||
Line 2,158: | Line 2,158: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
===Using Recurrence relation=== |
===Using Recurrence relation=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import math |
||
iterator b(): int = |
iterator b(): int = |
||
Line 2,217: | Line 2,217: | ||
===Using Bell triangle=== |
===Using Bell triangle=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">iterator b(): int = |
||
## Iterator yielding the bell numbers. |
## Iterator yielding the bell numbers. |
||
var row = @[1] |
var row = @[1] |
||
Line 2,311: | Line 2,311: | ||
=={{header|PARIGP}}== |
=={{header|PARIGP}}== |
||
From the code at OEIS A000110, |
From the code at OEIS A000110, |
||
<syntaxhighlight lang=text> |
<syntaxhighlight lang="text"> |
||
genit(maxx=50)={bell=List(); |
genit(maxx=50)={bell=List(); |
||
for(n=0,maxx,q=sum(k=0,n,stirling(n,k,2)); |
for(n=0,maxx,q=sum(k=0,n,stirling(n,k,2)); |
||
Line 2,322: | Line 2,322: | ||
{{Works with|Free Pascal}} |
{{Works with|Free Pascal}} |
||
Using bell's triangle. TIO.RUN up to 5000.See talk for more. |
Using bell's triangle. TIO.RUN up to 5000.See talk for more. |
||
<syntaxhighlight lang=pascal>program BellNumbers; |
<syntaxhighlight lang="pascal">program BellNumbers; |
||
{$Ifdef FPC} |
{$Ifdef FPC} |
||
{$optimization on,all} |
{$optimization on,all} |
||
Line 2,544: | Line 2,544: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
<syntaxhighlight lang=perl>use strict 'vars'; |
<syntaxhighlight lang="perl">use strict 'vars'; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 2,601: | Line 2,601: | ||
{{libheader|Phix/mpfr}} |
{{libheader|Phix/mpfr}} |
||
Started out as a translation of Go, but the main routine has now been completely replaced. |
Started out as a translation of Go, but the main routine has now been completely replaced. |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 2,654: | Line 2,654: | ||
'''First 18 Bell numbers and b(50).''' |
'''First 18 Bell numbers and b(50).''' |
||
(Port of the Sage solution at the OEIS A000110 page.) |
(Port of the Sage solution at the OEIS A000110 page.) |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picat">main => |
||
B50=b(50), |
B50=b(50), |
||
println(B50[1..18]), |
println(B50[1..18]), |
||
Line 2,679: | Line 2,679: | ||
'''Bell's Triangle (and the 50th Bell number)''' |
'''Bell's Triangle (and the 50th Bell number)''' |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picat">main => |
||
Tri = tri(50), |
Tri = tri(50), |
||
foreach(I in 1..10) |
foreach(I in 1..10) |
||
Line 2,718: | Line 2,718: | ||
{{trans|Prolog}} |
{{trans|Prolog}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picat">main :- |
||
bell(49, Bell), |
bell(49, Bell), |
||
printf("First 15 Bell numbers:\n"), |
printf("First 15 Bell numbers:\n"), |
||
Line 2,813: | Line 2,813: | ||
The symmetry constraint <code>value_precede_chain/2</code> ensures that a value N+1 is not placed in the list (X) before all the values 1..N has been placed ("seen") in the list. This handles the symmetry that the two sets {1,2} and {2,1} are to be considered the same. |
The symmetry constraint <code>value_precede_chain/2</code> ensures that a value N+1 is not placed in the list (X) before all the values 1..N has been placed ("seen") in the list. This handles the symmetry that the two sets {1,2} and {2,1} are to be considered the same. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picat">import cp. |
||
main => |
main => |
||
Line 2,886: | Line 2,886: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">(de bell (N) |
||
(make |
(make |
||
(setq L (link (list 1))) |
(setq L (link (list 1))) |
||
Line 2,938: | Line 2,938: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
<syntaxhighlight lang=prolog>bell(N, Bell):- |
<syntaxhighlight lang="prolog">bell(N, Bell):- |
||
bell(N, Bell, [], _). |
bell(N, Bell, [], _). |
||
Line 3,022: | Line 3,022: | ||
{{trans|D}} |
{{trans|D}} |
||
{{Works with|Python|2.7}} |
{{Works with|Python|2.7}} |
||
<syntaxhighlight lang=python>def bellTriangle(n): |
<syntaxhighlight lang="python">def bellTriangle(n): |
||
tri = [None] * n |
tri = [None] * n |
||
for i in xrange(n): |
for i in xrange(n): |
||
Line 3,079: | Line 3,079: | ||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
<syntaxhighlight lang=python>'''Bell numbers''' |
<syntaxhighlight lang="python">'''Bell numbers''' |
||
from itertools import accumulate, chain, islice |
from itertools import accumulate, chain, islice |
||
Line 3,312: | Line 3,312: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="quackery"> [ ' [ [ 1 ] ] ' [ 1 ] |
||
rot 1 - times |
rot 1 - times |
||
[ dup -1 peek nested |
[ dup -1 peek nested |
||
Line 3,355: | Line 3,355: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket>#lang racket |
<syntaxhighlight lang="racket">#lang racket |
||
(define (build-bell-row previous-row) |
(define (build-bell-row previous-row) |
||
Line 3,404: | Line 3,404: | ||
{{works with|Rakudo|2019.03}} |
{{works with|Rakudo|2019.03}} |
||
<syntaxhighlight lang=raku line> my @Aitkens-array = lazy [1], -> @b { |
<syntaxhighlight lang="raku" line> my @Aitkens-array = lazy [1], -> @b { |
||
my @c = @b.tail; |
my @c = @b.tail; |
||
@c.push: @b[$_] + @c[$_] for ^@b; |
@c.push: @b[$_] + @c[$_] for ^@b; |
||
Line 3,451: | Line 3,451: | ||
{{works with|Rakudo|2019.03}} |
{{works with|Rakudo|2019.03}} |
||
<syntaxhighlight lang=raku line>sub binomial { [*] ($^n … 0) Z/ 1 .. $^p } |
<syntaxhighlight lang="raku" line>sub binomial { [*] ($^n … 0) Z/ 1 .. $^p } |
||
my @bell = 1, -> *@s { [+] @s »*« @s.keys.map: { binomial(@s-1, $_) } } … *; |
my @bell = 1, -> *@s { [+] @s »*« @s.keys.map: { binomial(@s-1, $_) } } … *; |
||
Line 3,463: | Line 3,463: | ||
{{works with|Rakudo|2019.03}} |
{{works with|Rakudo|2019.03}} |
||
<syntaxhighlight lang=raku line>my @Stirling_numbers_of_the_second_kind = |
<syntaxhighlight lang="raku" line>my @Stirling_numbers_of_the_second_kind = |
||
(1,), |
(1,), |
||
{ (0, |@^last) »+« (|(@^last »*« @^last.keys), 0) } … * |
{ (0, |@^last) »+« (|(@^last »*« @^last.keys), 0) } … * |
||
Line 3,483: | Line 3,483: | ||
Also, see this task's [https://rosettacode.org/wiki/Talk:Bell_numbers#size_of_Bell_numbers ''discussion''] to view how the sizes of Bell numbers increase in relation to its index. |
Also, see this task's [https://rosettacode.org/wiki/Talk:Bell_numbers#size_of_Bell_numbers ''discussion''] to view how the sizes of Bell numbers increase in relation to its index. |
||
<syntaxhighlight lang=rexx>/*REXX program calculates and displays a range of Bell numbers (index starts at zero).*/ |
<syntaxhighlight lang="rexx">/*REXX program calculates and displays a range of Bell numbers (index starts at zero).*/ |
||
parse arg LO HI . /*obtain optional arguments from the CL*/ |
parse arg LO HI . /*obtain optional arguments from the CL*/ |
||
if LO=='' & HI=="" then do; LO=0; HI=14; end /*Not specified? Then use the default.*/ |
if LO=='' & HI=="" then do; LO=0; HI=14; end /*Not specified? Then use the default.*/ |
||
Line 3,532: | Line 3,532: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=ruby>def bellTriangle(n) |
<syntaxhighlight lang="ruby">def bellTriangle(n) |
||
tri = Array.new(n) |
tri = Array.new(n) |
||
for i in 0 .. n - 1 do |
for i in 0 .. n - 1 do |
||
Line 3,600: | Line 3,600: | ||
{{trans|D}} |
{{trans|D}} |
||
{{libheader|num|0.2}} |
{{libheader|num|0.2}} |
||
<syntaxhighlight lang=rust>use num::BigUint; |
<syntaxhighlight lang="rust">use num::BigUint; |
||
fn main() { |
fn main() { |
||
Line 3,655: | Line 3,655: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight lang=scala>import scala.collection.mutable.ListBuffer |
<syntaxhighlight lang="scala">import scala.collection.mutable.ListBuffer |
||
object BellNumbers { |
object BellNumbers { |
||
Line 3,744: | Line 3,744: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{works with|Chez Scheme}} |
{{works with|Chez Scheme}} |
||
<syntaxhighlight lang=scheme>; Given the remainder of the previous row and the final cons of the current row, |
<syntaxhighlight lang="scheme">; Given the remainder of the previous row and the final cons of the current row, |
||
; extend (in situ) the current row to be a complete row of the Bell triangle. |
; extend (in situ) the current row to be a complete row of the Bell triangle. |
||
; Return the final value in the extended row (for use in computing the following row). |
; Return the final value in the extended row (for use in computing the following row). |
||
Line 3,832: | Line 3,832: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
Built-in: |
Built-in: |
||
<syntaxhighlight lang=ruby>say 15.of { .bell }</syntaxhighlight> |
<syntaxhighlight lang="ruby">say 15.of { .bell }</syntaxhighlight> |
||
Formula as a sum of Stirling numbers of the second kind: |
Formula as a sum of Stirling numbers of the second kind: |
||
<syntaxhighlight lang=ruby>func bell(n) { sum(0..n, {|k| stirling2(n, k) }) }</syntaxhighlight> |
<syntaxhighlight lang="ruby">func bell(n) { sum(0..n, {|k| stirling2(n, k) }) }</syntaxhighlight> |
||
Via Aitken's array (optimized for space): |
Via Aitken's array (optimized for space): |
||
<syntaxhighlight lang=ruby>func bell_numbers (n) { |
<syntaxhighlight lang="ruby">func bell_numbers (n) { |
||
var acc = [] |
var acc = [] |
||
Line 3,862: | Line 3,862: | ||
Aitken's array: |
Aitken's array: |
||
<syntaxhighlight lang=ruby>func aitken_array (n) { |
<syntaxhighlight lang="ruby">func aitken_array (n) { |
||
var A = [1] |
var A = [1] |
||
Line 3,887: | Line 3,887: | ||
Aitken's array (recursive definition): |
Aitken's array (recursive definition): |
||
<syntaxhighlight lang=ruby>func A((0), (0)) { 1 } |
<syntaxhighlight lang="ruby">func A((0), (0)) { 1 } |
||
func A(n, (0)) { A(n-1, n-1) } |
func A(n, (0)) { A(n-1, n-1) } |
||
func A(n, k) is cached { A(n, k-1) + A(n-1, k-1) } |
func A(n, k) is cached { A(n, k-1) + A(n-1, k-1) } |
||
Line 3,901: | Line 3,901: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
<syntaxhighlight lang=swift>public struct BellTriangle<T: BinaryInteger> { |
<syntaxhighlight lang="swift">public struct BellTriangle<T: BinaryInteger> { |
||
@usableFromInline |
@usableFromInline |
||
var arr: [T] |
var arr: [T] |
||
Line 3,976: | Line 3,976: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
<syntaxhighlight lang=vbnet>Imports System.Numerics |
<syntaxhighlight lang="vbnet">Imports System.Numerics |
||
Imports System.Runtime.CompilerServices |
Imports System.Runtime.CompilerServices |
||
Line 4,063: | Line 4,063: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<syntaxhighlight lang=vlang>import math.big |
<syntaxhighlight lang="vlang">import math.big |
||
fn bell_triangle(n int) [][]big.Integer { |
fn bell_triangle(n int) [][]big.Integer { |
||
Line 4,132: | Line 4,132: | ||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<syntaxhighlight lang=ecmascript>import "/big" for BigInt |
<syntaxhighlight lang="ecmascript">import "/big" for BigInt |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 4,192: | Line 4,192: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
<syntaxhighlight lang=zkl>fcn bellTriangleW(start=1,wantRow=False){ // --> iterator |
<syntaxhighlight lang="zkl">fcn bellTriangleW(start=1,wantRow=False){ // --> iterator |
||
Walker.zero().tweak('wrap(row){ |
Walker.zero().tweak('wrap(row){ |
||
row.insert(0,row[-1]); |
row.insert(0,row[-1]); |
||
Line 4,199: | Line 4,199: | ||
}.fp(List(start))).push(start,start); |
}.fp(List(start))).push(start,start); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
<syntaxhighlight lang=zkl>println("First fifteen Bell numbers:"); |
<syntaxhighlight lang="zkl">println("First fifteen Bell numbers:"); |
||
bellTriangleW().walk(15).println();</syntaxhighlight> |
bellTriangleW().walk(15).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,206: | Line 4,206: | ||
L(1,1,2,5,15,52,203,877,4140,21147,115975,678570,4213597,27644437,190899322) |
L(1,1,2,5,15,52,203,877,4140,21147,115975,678570,4213597,27644437,190899322) |
||
</pre> |
</pre> |
||
<syntaxhighlight lang=zkl>println("Rows of the Bell Triangle:"); |
<syntaxhighlight lang="zkl">println("Rows of the Bell Triangle:"); |
||
bt:=bellTriangleW(1,True); do(11){ println(bt.next()) }</syntaxhighlight> |
bt:=bellTriangleW(1,True); do(11){ println(bt.next()) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,224: | Line 4,224: | ||
</pre> |
</pre> |
||
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library |
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library |
||
<syntaxhighlight lang=zkl>print("The fiftieth Bell number: "); |
<syntaxhighlight lang="zkl">print("The fiftieth Bell number: "); |
||
var [const] BI=Import("zklBigNum"); // libGMP |
var [const] BI=Import("zklBigNum"); // libGMP |
||
bellTriangleW(BI(1)).drop(50).value.println();</syntaxhighlight> |
bellTriangleW(BI(1)).drop(50).value.println();</syntaxhighlight> |