June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -3,9 +3,10 @@ Print out the first   '''15'''   Catalan numbers by extracting them fr
;See:
* &nbsp; [http://milan.milanovic.org/math/english/fibo/fibo4.html Catalan Numbers and the Pascal Triangle]. &nbsp; &nbsp; This method enables calculation of Catalan Numbers using only addition and subtraction. <br> &nbsp; &nbsp; '''The (above) link is broken.'''
* &nbsp; [https://archive.is/0IrNp Catalan Numbers and the Pascal Triangle]. <!-- Relation Pascal Triangle and the Catalan Numbers Radoslav Jovanovic --> &nbsp; &nbsp; This method enables calculation of Catalan Numbers using only addition and subtraction.
<!-- '''http://milan.milanovic.org/math/english/fibo/fibo4.html is broken. -->
* &nbsp; [http://mathworld.wolfram.com/CatalansTriangle.html Catalan's Triangle] for a Number Triangle that generates Catalan Numbers using only addition.
* &nbsp; Sequence [http://oeis.org/A000108 A000108] on OEIS has a lot of information on Catalan Numbers.
;Related Tasks:
[[Pascal's triangle]]

View file

@ -0,0 +1,14 @@
USING: arrays grouping io kernel math prettyprint sequences ;
IN: rosetta-code.catalan-pascal
: next-row ( seq -- seq' )
2 clump [ sum ] map 1 prefix 1 suffix ;
: pascal ( n -- seq )
1 - { { 1 } } swap [ dup last next-row suffix ] times ;
15 2 * pascal [ length odd? ] filter [
dup length 1 = [ 1 ]
[ dup midpoint@ dup 1 + 2array swap nths first2 - ] if
pprint bl
] each drop

View file

@ -0,0 +1,18 @@
package main
import "fmt"
func main() {
const n = 15
t := [n + 2]uint64{0, 1}
for i := 1; i <= n; i++ {
for j := i; j > 1; j-- {
t[j] += t[j-1]
}
t[i+1] = t[i]
for j := i + 1; j > 1; j-- {
t[j] += t[j-1]
}
fmt.Printf("%2d : %d\n", i, t[i+1]-t[i])
}
}

View file

@ -0,0 +1,12 @@
let catalan : int ref = ref 0 in
Printf.printf "%d ," 1 ;
for i = 2 to 9 do
let nm : int ref = ref 1 in
let den : int ref = ref 1 in
for k = 2 to i do
nm := (!nm)*(i+k);
den := (!den)*k;
catalan := (!nm)/(!den) ;
done;
print_int (!catalan); print_string "," ;
done;;

View file

@ -1,2 +1,7 @@
: pascal(n) [ 1 ] #[ dup 0 + 0 rot + zipWith(#+) ] times(n) ;
: catalan(n) pascal(n 2 * ) at(n 1+) n 1+ / ;
import: mapping
: pascal( n -- [] )
[ 1 ] n #[ dup [ 0 ] + [ 0 ] rot + zipWith( #+ ) ] times ;
: catalan( n -- m )
n 2 * pascal at( n 1+ ) n 1+ / ;

View file

@ -0,0 +1,5 @@
def catalan(n: Int): Int =
if (n <= 1) 1
else (0 until n).map(i => catalan(i) * catalan(n - i - 1)).sum
(1 to 15).map(catalan(_))