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– Shaggy2019年11月22日 23:58:57 +00:00Commented Nov 22, 2019 at 23:58
-
1\$\begingroup\$ Can I output HTML that would generated the required text? \$\endgroup\$lyxal– lyxal ♦2019年11月23日 03:09:00 +00:00Commented Nov 23, 2019 at 3:09
-
7\$\begingroup\$ Why do we have to use colored characters? Can we e.g. use differemt letters? \$\endgroup\$the default.– the default.2019年11月23日 04:02:06 +00:00Commented Nov 23, 2019 at 4:02
-
4\$\begingroup\$ Agree with pronoun. To me, the colored text bit is "Adding unnecessary fluff". \$\endgroup\$Chas Brown– Chas Brown2019年11月23日 05:56:32 +00:00Commented Nov 23, 2019 at 5:56
-
\$\begingroup\$ OK see the edits) \$\endgroup\$Alexey Burdin– Alexey Burdin2019年11月23日 10:12:35 +00:00Commented 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)\$
Formula
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\$).
Commented
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\$Alexey Burdin– Alexey Burdin2019年11月23日 10:27:30 +00:00Commented 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 bytess*a/c-sis pretty much impressive. Can you write some comments on how the things work?) \$\endgroup\$Alexey Burdin– Alexey Burdin2019年11月23日 10:59:46 +00:00Commented 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 usingAasLwould be better. And I do especially want to know hows = s * a / c - sgenerates the pascal triangle) Thanks \$\endgroup\$Alexey Burdin– Alexey Burdin2019年11月23日 13:13:35 +00:00Commented Nov 23, 2019 at 13:13 -
\$\begingroup\$ Hmm thanks for incorporating my thoughts into your solution.
L?L:9is not needed now?) \$\endgroup\$Alexey Burdin– Alexey Burdin2019年11月23日 13:44:35 +00:00Commented 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– Arnauld2019年11月23日 13:47:11 +00:00Commented Nov 23, 2019 at 13:47
Python 2, (削除) 158 (削除ここまで) (削除) 134 (削除ここまで) 132 bytes
n,d=input()
o=r=[1]
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
22 bytes from tips by Jonathan Allan.
Uses 01234567 as the 8 characters instead of 8 colors.
-
\$\begingroup\$
'%d'%ito save 7 TIO. \$\endgroup\$Jonathan Allan– Jonathan Allan2019年11月24日 00:40:47 +00:00Commented Nov 24, 2019 at 0:40 -
\$\begingroup\$
r=[1]+map(sum,zip(r,r[1:]))+[1]for 13 more :) \$\endgroup\$Jonathan Allan– Jonathan Allan2019年11月24日 00:46:41 +00:00Commented Nov 24, 2019 at 0:46 -
\$\begingroup\$ Ah backticks - even better :) \$\endgroup\$Jonathan Allan– Jonathan Allan2019年11月24日 00:51:52 +00:00Commented 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– Chas Brown2019年11月24日 00:53:37 +00:00Commented Nov 24, 2019 at 0:53 -
\$\begingroup\$ 2 more with
o=r=[1]andr=o+map(sum,zip(r,r[1:]))+o\$\endgroup\$Jonathan Allan– Jonathan Allan2019年11月24日 01:00:42 +00:00Commented Nov 24, 2019 at 1:00
05AB1E, (削除) 17 (削除ここまで) 16 bytes
ÝεyÝcI7LmδÖO}».c
Ý # 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, input2, ..., 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
1λ0ドルaDÁ+}εI7LmδÖOoJ}.c
Jelly, 23 bytes
1Ø0j+ƝƊ8СṖọ«7ịØaḤz6ZṚ€
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
FNNÝcεU7ÝR.ΔmXsÖ])».c
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).
Explanation:
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– Grimmy2019年11月25日 14:12:50 +00:00Commented 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 aboutc. ;) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年11月25日 14:15:26 +00:00Commented Nov 25, 2019 at 14:15 -
1\$\begingroup\$ Yup I'm down to 17 now \$\endgroup\$Grimmy– Grimmy2019年11月25日 14:17:26 +00:00Commented Nov 25, 2019 at 14:17
-
\$\begingroup\$ @Grimmy Nice! As always you're completely overshadowing me with your lower byte-count, haha. ;p \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年11月25日 14:18:28 +00:00Commented Nov 25, 2019 at 14:18
-
1\$\begingroup\$ Note that all the bytes I save are on the color computation.
FNNÝcI7LmδÖO})».cwould also be 17 and much closer to your approach. \$\endgroup\$Grimmy– Grimmy2019年11月25日 14:24:38 +00:00Commented Nov 25, 2019 at 14:24
Japt, (削除) 41 (削除ここまで) (削除) 36 (削除ここまで) (削除) 32 (削除ここまで) (削除) 29 (削除ここまで) 25 bytes
_p1 ä+T}h[]à)m£_XuVpZa7}f
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\$ @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\$AZTECCO– AZTECCO2019年11月26日 19:14:46 +00:00Commented 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– Shaggy2019年11月26日 20:53:54 +00:00Commented Nov 26, 2019 at 20:53
-
-
\$\begingroup\$ Or, better yet, without needing to do anything in the footer:
Ë£_XuVpZ}fa7\$\endgroup\$Shaggy– Shaggy2019年11月26日 21:12:56 +00:00Commented Nov 26, 2019 at 21:12
Charcoal, 36 bytes
×ばつ11ΣE8¬%κXθμ↙⊞υ0UMυ+κ§υ⊖λ
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:
Nθ
Input the base.
⊞υ1
Start the first row with a single 1.
FN«
Loop over each row.
×ばつ11ΣE8¬%κXθμ↙
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.
⊞υ0UMυ+κ§υ⊖λ
Add another column to the row and calculate each row as the sum of the two cells above.