Pascal's triangle: Difference between revisions

m
→‎explanation: add indents
No edit summary
m (→‎explanation: add indents)
Line 484:
Of course, the triangle itself is simply the table of number-of-combinations, for the first N non-negative integers.
 
That is, [[wp:Combination|C(n,k)]] for all <tt>n,k ∈ [0 .. n)</tt>. J's notation for <tt>C(n,k)</tt> is <ttcode>k ! n</ttcode> (mnemonic: combinations are closely related to factorials, which are denoted by <tt>!</tt> in math).
 
So, for example, the number of ways to choose a poker hand (5 cards from the deck of 52):
Line 490:
2598960</lang>
 
So <ttcode>!</ttcode> is the mathematical choose function. What of <ttcode>/~@i.</ttcode>? Well, you can think of <ttcode>/~</ttcode> as "table of" and <ttcode>@i.</ttcode> "the first N non-negative integers (i.e. 0 .. N-1)".
 
So, for example, here's the multiplication table you had to memorize in first grade:
<lang j> */~@i. 10
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81</lang>
 
and here's the addition table for 0 to 4
<lang j> +/~@i. 4
0 1 2 3
1 2 3 4
Line 512:
3 4 5 6</lang>
 
Similarly, <ttcode>!/~@i.</ttcode> is the number-of-combinations table, or the "choose" table:
<lang j> !/~@i. 5
1 1 1 1 1
0 1 2 3 4
Line 521:
 
Of course, to format it nicely, you need to do a little more work:
<lang j> ([:'0'&=`(,:&' ')} -@|. |."_1 [: ":@|: !/~)@i. 5
1
1 1
892

edits