8
\$\begingroup\$

Print this tree:

           1           
         1 2 1         
        1 3 3 1        
       1 4 6 4 1       
     1 5 10 10 5 1     
        15 20 15       
    7 21 35 35 21 7    
   8 28 56 70 56 28 8  
9 36 84 126 126 84 36 9
          252          
        462 462        
          924          
       1716 1716       

This is , so shortest code in chars wins!

\$\endgroup\$
  • \$\begingroup\$ Related: Pascal's triangle \$\endgroup\$ – Luis Mendo Jul 12 at 22:26
  • 2
    \$\begingroup\$ With the leading spaces or doesn't it matter? \$\endgroup\$ – xash Jul 12 at 22:32
  • \$\begingroup\$ No, it doesnt matter \$\endgroup\$ – Nip Dip Jul 13 at 1:32
6
\$\begingroup\$

J, 67 bytes

(1785951#:~13#7)(0(,~' '#~12-<.@-:@#)@":-@[}.}.)&>1;2}.<@(!{:)\i.14

The pascal's triangle part is adapted from @ephemient's answer here.

Try it online!

How it works

<@(!{:)\i.14

The first 14 rows of the triangle.

1;2}.

Drop the first two rows, prepend 1.

(1785951#:~13#7)

0 0 0 0 0 2 1 1 1 5 5 6 6 stored in base 7.

":-@[}.}.)&>1

Unbox, drop the first N and last N elements from each row, where N is the corresponding item from the base 7 array before.

0(…)@":

Convert each row to string and …

12-<.@-:@#

Count its length, halve and round it down.

,~' '#~

Prepend that amount of spaces before each row.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Is there a way I can learn this coding language, it seems interesting \$\endgroup\$ – Nip Dip Jul 13 at 1:34
  • 1
    \$\begingroup\$ @NipDip The J website has many resources. There is the J primer, J for C Programmers, and Learning J, among much other content on the Wiki. \$\endgroup\$ – Jonah Jul 13 at 3:14
  • \$\begingroup\$ ok, thanks for the tip \$\endgroup\$ – Nip Dip Jul 13 at 4:20
6
\$\begingroup\$

Python 2, 118 bytes

n=1
exec"a=n/6+n/10*4+(n==6);print' '.join(str(((2**n+1)**n>>n*k)%2**n)for k in range(a,n+1%n-a)).center(23);n+=1;"*13

Try it online!

Expresses binomial coefficients inline like in my tip here with binom(n,k)=((2**n+1)**n>>n*k)%2**n. The number of entries cut off on each side for the n'th row (one-indexed) is expressed as n/6+n/10*4+(n==6). For the first row, one additional entry on the right is cut off.

Here's a slightly more readable version without an exec so that the syntax highlighting works:

119 bytes

n=1
while n<14:a=n/6+n/10*4+(n==6);print' '.join(str(((2**n+1)**n>>n*k)%2**n)for k in range(a,n+1%n-a)).center(23);n+=1

Try it online!

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 26 bytes

13LεDÝc•6hö¢ðU•RNèF¦¨]»¦.c

Try it online.

Explanation:

13L            # Push a list in the range [1,13]
   ε           # Map each value `y` to:
    D          #  Duplicate the value `y`
     Ý         #  Pop and push a list in the range [0,`y`]
      c        #  Take the binomial coefficient of `y` with each value in this list
               #  (we now have the 0-based `y`'th Pascal row)
    •6hö¢ðU•   #  Push compressed integer 6655111200000
            R  #  Reverse it to "0000021115566"
    Nè         #  Index the map-index into it to get the `N`'th digit
      F        #  Loop that many times:
       ¦¨      #   Remove both the first and last item of the current Pascal row list
   ]           # Close both the inner loop and map
    »          # Join each inner list by spaces, and then each string by newlines
     ¦         # Remove the leading 1 on the very first line
      .c       # Left-focused centralize the newline-delimited string
               # (after which the result is output implicitly)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why •6hö¢ðU• is 6655111200000.

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES8), 137 bytes

_=>[a=[1],..."000021115566"].map((v,n)=>(s=(a=[i=1,...a.map(v=>v+~~a[i++])]).slice(v,n+2+!n-v).join` `).padStart(23+s.length>>1)).join`
`

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Charcoal, 57 bytes

F³⊞υ﹪ι²F”)⧴→↨w﹪f”«UMυ⁺κ§υ⊖λ⊞υ⁰F›ⅉ⁰≔⪫✂υIι±Iι¹ ι⟦⁺× ⊘⁻²⁴Lιι

Try it online! Link is to verbose version of code. Explanation:

F³⊞υ﹪ι²

Start by preparing a list 0 1 0. (Charcoal's indexing is cyclic, so rather than letting it wrap I need to provide a safety margin.)

F”)⧴→↨w﹪f”«

Loop over the compressed string 111113222667 which represents the number of entries to slice off each side (except on the first iteration).

UMυ⁺κ§υ⊖λ

Add each element to the next element (so the first element remains 0).

⊞υ⁰

Append another 0 to the end. (This is done here so that it can be easily sliced off again.)

F›ⅉ⁰≔⪫✂υIι±Iι¹ ι

Except on the first loop, slice off the given number of entries from the start and the end (this has to be nonzero to work, which is why we already pushed the 0 only to slice it off again, and also why we have an extra 0 at the start, which otherwise wouldn't be necessary). On the first loop, the 1 just ends up being the output string.

⟦⁺× ⊘⁻²⁴Lιι

Centre the string in a width of 24 (23 would round the sixth and eighth lines down which is not what we want) and output each string on its own line.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Ruby, 91 bytes

66551112000090.digits.map{|i|n=0;$*.map!{|j|n+n=j}<<1;puts ($*[i..~i]*' ').center 23if i<9}

Try it online!

Iterates over the digits of 66551112000090 (from the right), one digit for each row of the tree. Each digit, with the exception of the 9, specifies the number of elements to be omitted from the beginning and end of the corresponding row of Pascal's triangle. The 9 is a distinct digit used to suppress printing of the second row of the triangle.

The Pascal's triangle code is based on this answer by @manatwork, golfed further by abusing the predefined array $*. $* normally stores the list of command-line options. Since we use no command-line options, $* is initialised to the empty array.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

///, 196 bytes

/h/ 3//g/ 2//f/ 1//e/aa//d/
a//c/  //b/ea//a/c /bc1bc
b1gfbdac1hhfecdaf 4 6 4fe dc1 5f0f0 5facdac15g0f5e d 7g1h5h5g1 7a d8g8 56 70 56g8 8c
9h6 84f26f26 84h6 9
bg52b dac462 462ec
b 924b daf716f716e

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Is this a direct print, or an algorithmic solution? \$\endgroup\$ – Razetime Aug 23 at 12:03
  • \$\begingroup\$ @Razetime Just direct print with many substitutions. \$\endgroup\$ – nph Aug 23 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.