Floyd's triangle: Difference between revisions

Content deleted Content added
Sonia (talk | contribs)
Rdm (talk | contribs)
Line 74: Line 74:
<lang J>require 'strings'
<lang J>require 'strings'
floyd=: [: rplc&(' 0';' ')"1@":@(* ($ $ +/\@,)) >:/~@:i.</lang>
floyd=: [: rplc&(' 0';' ')"1@":@(* ($ $ +/\@,)) >:/~@:i.</lang>

Note, the parenthesis around ($ $ +/\@,) is optional, and only included for emphasis.


Example use:
Example use:
Line 98: Line 100:
79 80 81 82 83 84 85 86 87 88 89 90 91
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105</lang>
92 93 94 95 96 97 98 99 100 101 102 103 104 105</lang>

How it works:

First, we create a square lower triangular matrix with our argument as the length of one side. We have 1s along the diagonal and the lower triangle, and 0s for the upper triangle.

Second, we create a running sum of these values (treating rows as being adjacent horizontally for this purpose). Then, we multiply this result by our lower triangular matrix (forcing the upper triangle to be 0s).

Then, we format the matrix as text (which gives us the required vertical alignment), and in each row we replace each space followed by a zero with two spaces.

Efficiency note: In a measurement of time used: in floyd 100, 80% the time here goes into the string manipulations -- sequential additions and multiplications are cheap. In floyd 1000 this jumps to 98% of the time.


=={{header|Python}}==
=={{header|Python}}==