Floyd's triangle: Difference between revisions

Content added Content deleted
(→‎{{header|AppleScript}}: Updated primitives)
Line 2,253: Line 2,253:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
<lang julia>#floyd(n) creates an n-row floyd's triangle counting from 1 to (n/2+.5)*n

function floyd(n)
<lang julia></lang>
x = 1
dig(x,line,n) = (while line < n; x+=line; line+= 1 end; return ndigits(x)+1)
for line = 1:n, i = 1:line; print(lpad(x,dig(x,line,n)," ")); x+=1; i==line && print("\n") end
end</lang>
<pre>julia> floyd(5)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15


{{out}}
julia> floyd(14)
1
<pre> 1
2 3
2 3
4 5 6
4 5 6
Line 2,281: Line 2,272:
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</pre>
92 93 94 95 96 97 98 99 100 101 102 103 104 105</pre>

Here is another solution that makes use of the fact that the number in the (i,j)th position in the array is equal to the sum of j and the binomial coefficient (j,2). This
number should be padded according to the number of digits
in the coefficient (n,j).
<lang julia>floyd(n) =
pprint([join([lpad(j+binomial(i,2), (j==1?0:1)+ndigits(j+binomial(n,2)), " ")
for j=1:i])
for i=1:n])

pprint(matrix) = for i = 1:size(matrix,1) println(join(matrix[i,:])) end
</lang>
{{Out}}
<pre>julia> floyd(5)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==