<commit_before>! Copyright (c) 2012 Anonymous
! See http://factorcode.org/license.txt for BSD license.
USING: grouping kernel math sequences ;
IN: rosetta-code.pascals-triangle

! http://rosettacode.org/wiki/Pascal%27s_triangle

! Pascal's triangle is an interesting math concept. Its first few rows look like this:
!    1
!   1 1
!  1 2 1
! 1 3 3 1

! where each element of each row is either 1 or the sum of the
! two elements right above it. For example, the next row would be
! 1 (since the first element of each row doesn't have two elements
! above it), 4 (1 + 3), 6 (3 + 3), 4 (3 + 1), and 1 (since the
! last element of each row doesn't have two elements above it).
! Each row n (starting with row 0 at the top) shows the
! coefficients of the binomial expansion of (x + y)n.

! Write a function that prints out the first n rows of the
! triangle (with f(1) yielding the row consisting of only the
! element 1). This can be done either by summing elements from the
! previous rows or using a binary coefficient or combination
! function. Behavior for n <= 0 does not need to be uniform, but
! should be noted.

: (pascal) ( seq -- newseq )
    dup last 0 prefix 0 suffix 2 <clumps> [ sum ] map suffix ;

: pascal ( n -- seq )
    1 - { { 1 } } swap [ (pascal) ] times ;<commit_msg>rosetta-code.pascals-triangle: adding a coefficients method.<commit_after>! Copyright (c) 2012 Anonymous
! See http://factorcode.org/license.txt for BSD license.
USING: grouping kernel locals math math.ranges sequences ;
IN: rosetta-code.pascals-triangle

! http://rosettacode.org/wiki/Pascal%27s_triangle

! Pascal's triangle is an interesting math concept. Its first few rows look like this:
!    1
!   1 1
!  1 2 1
! 1 3 3 1

! where each element of each row is either 1 or the sum of the
! two elements right above it. For example, the next row would be
! 1 (since the first element of each row doesn't have two elements
! above it), 4 (1 + 3), 6 (3 + 3), 4 (3 + 1), and 1 (since the
! last element of each row doesn't have two elements above it).
! Each row n (starting with row 0 at the top) shows the
! coefficients of the binomial expansion of (x + y)n.

! Write a function that prints out the first n rows of the
! triangle (with f(1) yielding the row consisting of only the
! element 1). This can be done either by summing elements from the
! previous rows or using a binary coefficient or combination
! function. Behavior for n <= 0 does not need to be uniform, but
! should be noted.

:: pascal-coefficients ( n -- seq )
    1 n [1,b] [
        dupd [ n swap - * ] [ /i ] bi swap
    ] map nip ;

: (pascal) ( seq -- newseq )
    dup last 0 prefix 0 suffix 2 <clumps> [ sum ] map suffix ;

: pascal ( n -- seq )
    1 - { { 1 } } swap [ (pascal) ] times ;
{'fraction_non_alphanumeric': 0.09216255442670537, 'fraction_numerical': 0.027576197387518143, 'mean_word_length': 3.222052067381317, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4821445', 'n_tokens_mistral': 917, 'n_tokens_neox': 828, 'n_words': 432}