Although what is a Pascal's triangle is well-known and we already can generate it, the task is now different:
Output \$n\$ first lines of the Pascal's triangle as colored bricks.
Color number is computed as maximal power of \$d\$, that divides the needed-to-color element or \$7\$ if power is more than \$7\$.
Numbered colors are (\$0\$-based): default, red, green, yellow, blue, magenta, cyan, white (default color is 0th, red is 1st, .. white is 7th).

  • Any space-filling (I mean not .,' or " or smth small) character
    can be used for bricks, even '\u2588'.
  • Whitespace before or after the bricks are optional.
  • Brick horizontal length (\$1\$ or \$2\$) is your choice.
  • You may choose any convenient output method -- ansi text or graphics or HTML or whatever
  • If you choose output letters or color numbers instead of coloring the triangle itself, you should specify in the text of your answer which character corresponds to which color, but output wouldn't look so nice)

Input Integers \$n\$ and \$d>1\$ in any convenient method.
This is code-golf, the shortest code in each language wins.

  • \$\begingroup\$ Related \$\endgroup\$
    – Shaggy
    Commented Nov 22, 2019 at 23:58
  • 1
    \$\begingroup\$ Can I output HTML that would generated the required text? \$\endgroup\$
    – lyxal
    Commented Nov 23, 2019 at 3:09
  • 7
    \$\begingroup\$ Why do we have to use colored characters? Can we e.g. use differemt letters? \$\endgroup\$ Commented Nov 23, 2019 at 4:02
  • 4
    \$\begingroup\$ Agree with pronoun. To me, the colored text bit is "Adding unnecessary fluff". \$\endgroup\$
    – Chas Brown
    Commented Nov 23, 2019 at 5:56
  • \$\begingroup\$ OK see the edits) \$\endgroup\$ Commented Nov 23, 2019 at 10:12

8 Answers 8


C (gcc),  157 153 143 139  135 bytes

Saved several bytes thanks to @AlexeyBurdin
Saved  4  8 more bytes thanks to @ceilingcat

Outputs with ANSI colors.

long p,a,s,c,L;f(n,d){for(a=0;c=a++-n;puts(""))for(;p=c<a;printf(c<0?" ":"\e[3%dm@@",L),s=++c<1?1:s*a/c-s)for(L=0;p*=d,L<7&s%p<1;L++);}

Try it online! (no colors on TIO)

Example output for \$f(40,2)\$



At row \$r\$ and column \$c-1\$, we have:

$$P_{r,c-1}={r\choose c-1}=\frac{r!}{(c-1)!(r-c+1)!}$$

On the same row and the next column \$c\$, we have:

$$\begin{align}P_{r,c}={r\choose c}&=\frac{r!}{c!(r-c)!}\\ &=\frac{r-c+1}{c}\times\frac{r!}{(c-1)!(r-c+1)!}\\ &=\frac{r-c+1}{c}\times P_{r,c-1}\\ &=\frac{r+1}{c}P_{r,c-1}-P_{r,c-1}\end{align}$$

which is translated as s = s * a / c - s in the C code (with \$a=r+1\$).


long p, a, s, c, L;               // declare a few 64-bit integers
f(n, d) {                         // n = number of rows, d = colorization parameter
  for(a = 0; a++ < n; puts(""))   // for a = 1 to n, with a linefeed added after each
    for(                          // iteration:
      c = a + ~n;                 //   for c = a - n - 1 to a - 1:
      p = c < a;                  //
      printf(                     //     update the output after each iteration:
        c < 0 ?                   //       if c is negative:
          " "                     //         just append a space
        :                         //       else:
          "\e[3%dm@@",            //         append the ANSI color code, followed by '@@'
        L                         //       set the 2nd digit of the color code
      ),                          //
      s = ++c < 1 ? 1             //     increment c; set s to 1 while c is less than 1
                  : s * a / c - s //     then, update s to s * a / c - s
    )                             //
      for(                        //       compute the color L:
        L = 0;                    //         start with L = 0
        p *= d,                   //         multiply p by d
        L < 7 & s % p < 1;        //         stop if L = 7 or p does not divide s
        L++                       //         increment L
      );                          //
}                                 //
  • \$\begingroup\$ Impressive. I thought default color is 39, not 97, so you can golf 3 bytes, nm. \$\endgroup\$ Commented Nov 23, 2019 at 10:27
  • \$\begingroup\$ Is the case of power of d being more than 7 really covered by l=s%p?l:A? Anyway, computing the triangle with only 7 bytes s*a/c-s is pretty much impressive. Can you write some comments on how the things work?) \$\endgroup\$ Commented Nov 23, 2019 at 10:59
  • \$\begingroup\$ Not sure it's 100% working. Maybe for(A=0,p=d;!(s%p);A++,p*=d) and using A as L would be better. And I do especially want to know how s = s * a / c - s generates the pascal triangle) Thanks \$\endgroup\$ Commented Nov 23, 2019 at 13:13
  • \$\begingroup\$ Hmm thanks for incorporating my thoughts into your solution. L?L:9 is not needed now?) \$\endgroup\$ Commented Nov 23, 2019 at 13:44
  • \$\begingroup\$ @AlexeyBurdin Given that using colors is now optional, I made the assumption that we may just as well use any color for the 'default' one. But I can revert that change if that's invalid. \$\endgroup\$
    – Arnauld
    Commented Nov 23, 2019 at 13:47

Python 2, 158 134 132 bytes

exec"print''.join([`i`for i in range(8)if c%d**i<1][-1]*2for c in r).center(2*n);r=o+map(sum,zip(r,r[1:]))+o;"*n

Try it online!

22 bytes from tips by Jonathan Allan.

Uses 01234567 as the 8 characters instead of 8 colors.

  • \$\begingroup\$ '%d'%i to save 7 TIO. \$\endgroup\$ Commented Nov 24, 2019 at 0:40
  • \$\begingroup\$ r=[1]+map(sum,zip(r,r[1:]))+[1] for 13 more :) \$\endgroup\$ Commented Nov 24, 2019 at 0:46
  • \$\begingroup\$ Ah backticks - even better :) \$\endgroup\$ Commented Nov 24, 2019 at 0:51
  • \$\begingroup\$ @Jonathan Allan: Yep! Good call on the others; I was spacing with the r[:-1]... :) \$\endgroup\$
    – Chas Brown
    Commented Nov 24, 2019 at 0:53
  • \$\begingroup\$ 2 more with o=r=[1] and r=o+map(sum,zip(r,r[1:]))+o \$\endgroup\$ Commented Nov 24, 2019 at 1:00

05AB1E, 17 16 bytes


Try it online!

Ý                 # range 0..input
 ε          }     # for each number y in that range:
  yÝ              #  range 0..y
    c             #  binomial coefficient (yields a row of the triangle)
     I            #  second input
      7L          #  range 1..7
        m         #  power (yields [input, input², ..., input**7])
         δÖ       #  double-vectorized divisible-by
           O      #  sum each inner list
»                 # join by newlines, joining sublists by spaces
 .c               # center

05AB1E, 22 bytes, no binomial coefficient built-in


Try it online!


Jelly, 23 bytes


Try it online!

A full program taking the number of rows as its first argument and the power as its second. Outputs the triangle to STDOUT. Colours are represented by z for 0/default and a to g for 1 to 7+.


05AB1E, 24 21 bytes


First input is the amount of rows n, second input the power-base d.
Outputs [0,7] instead of the colors, and only outputs a single digit for the bricks (with spaces).

Try it online.


F                # Loop in the range [0, (implicit) input n):
 NN              #  Push the current loop-index twice
   Ý             #  Pop the top one, and create a list in the range [0, index]
    c            #  Calculate the binomial coefficient of the index and this ranged list
                 #   i.e. 10 choose [0,1,2,3,4,5,6,7,8,9,10]
                 #    → [1,9,36,84,126,126,84,36,9,1]
    ε            #  Map each value to:
     U           #   Pop and store the current value in variable `X`
      7Ý         #   Push a list in the range [0,7]
        R        #   Reverse it to make the range [7,0]
         .Δ      #   Find the first digit which is truthy for:
           m     #    Take the (implicit) input d to the power of the current digit
            Xs   #    Push variable `X` and swap the two values
              Ö  #    Check if `X` is divisible by this digit ** `d`
]                # Close the find_first; map; and loop
 )               # Wrap all lists into a list
  »              # Join each inner list by spaces, and then each string by newlines
   .c            # Centralize the lines by padding leading spaces
                 # (after which the result is output implicitly)
  • \$\begingroup\$ How did I miss c >.> \$\endgroup\$
    – Grimmy
    Commented Nov 25, 2019 at 14:12
  • 1
    \$\begingroup\$ @Grimmy I remembered golfing an 05AB1E answer for the Pascal Triangle challenge which was using c. I'm not too happy with my εU7ÝR.ΔmXsÖ] part, though. But I'm looking forward seeing a potential golf of your answer now that you know about c. ;) \$\endgroup\$ Commented Nov 25, 2019 at 14:15
  • 1
    \$\begingroup\$ Yup I'm down to 17 now \$\endgroup\$
    – Grimmy
    Commented Nov 25, 2019 at 14:17
  • \$\begingroup\$ @Grimmy Nice! As always you're completely overshadowing me with your lower byte-count, haha. ;p \$\endgroup\$ Commented Nov 25, 2019 at 14:18
  • 1
    \$\begingroup\$ Note that all the bytes I save are on the color computation. FNNÝcI7LmδÖO})».c would also be 17 and much closer to your approach. \$\endgroup\$
    – Grimmy
    Commented Nov 25, 2019 at 14:24

Japt, 41 36 32 29 25 bytes

_p1 ä+T}h[]à)m£_XuVpZa7}f

Try it

      h   )  // run input times
       []à   // starting with empty 2d array (permutations of [])

_    }       // function that:
 p1          // appends 1
    T        // and prepends 0
  ä+         // sums consecutive elements

m            // for each line
 £           // for each element
           f // find first number (starting at 0) that return false this function:  
  _       }  // f(Z)
   Xu        // triangle element modulo
     Vp      // 2nd input raised to  
       Za8   // absolute difference 

Uses numbers [7...0] as colors Footer inverts colors to [0...7] for simpler output

Saved a lot thanks to @Shaggy

  • 1
    \$\begingroup\$ 24 bytes? \$\endgroup\$
    – Shaggy
    Commented Nov 26, 2019 at 16:00
  • \$\begingroup\$ @Shaggy good! I think you should post your own answer since it's a different approach, anyway I'm gonna steal the ä+0 from you , I also think outputting [7...0] is valid but I'm gonna leave as [0...7] bcuz it's more clear \$\endgroup\$
    Commented Nov 26, 2019 at 19:14
  • \$\begingroup\$ No, you work away, buddy - all's I've done is golf your method. Besides, I haven't read the spec nor fully tested my version! \$\endgroup\$
    – Shaggy
    Commented Nov 26, 2019 at 20:53
  • \$\begingroup\$ You could change the digits used in the footer, like so. Or, if you do want to retain them in your solution, you can still save a byte with £7a_ to avoid the last space. \$\endgroup\$
    – Shaggy
    Commented Nov 26, 2019 at 20:55
  • \$\begingroup\$ Or, better yet, without needing to do anything in the footer: Ë£_XuVpZ}fa7 \$\endgroup\$
    – Shaggy
    Commented Nov 26, 2019 at 21:12

Charcoal, 36 bytes


Try it online! Link is to verbose version of code. Takes the number of rows as the second input. Output uses digits 1-8 to represent the colours (1 = default). Explanation:


Input the base.


Start the first row with a single 1.


Loop over each row.


For each cell, determine its divisibility by the first 8 powers of the base, and take the sum. Then multiply by 11 to duplicate the digit. (Alternatively, casting to string and then duplicating the digit also works.) Don't move the cursor while printing, instead finish by moving the cursor down and left.


Add another column to the row and calculate each row as the sum of the two cells above.


Ruby, 123 bytes

->n,d{(0..n).map{|w|' '*(n-w)+(0..w).map{|i|"\e[#{30+(b=[*1..w].combination(i).size).downto(0).find{|w|b%d**w<1}}m##"}*''}}

Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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