Jump to content

Floyd's triangle: Difference between revisions

(→‎{{header|AppleScript}}: Updated primitives)
Line 2,253:
 
=={{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>floyd(n) = </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
 
{{Outout}}
julia> floyd(14)
<pre> 1
2 3
4 5 6
Line 2,281 ⟶ 2,272:
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>
 
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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.