Triangular numbers: Difference between revisions
Content added Content deleted
(→{{header|Raku}}: perpend Free Pascal) |
(J) |
||
Line 72: | Line 72: | ||
;* [[Evaluate_binomial_coefficients|Related task: Evaluate binomial coefficients]] |
;* [[Evaluate_binomial_coefficients|Related task: Evaluate binomial coefficients]] |
||
;* [[Pascal's_triangle|Related task: Pascal's triangle]] |
;* [[Pascal's_triangle|Related task: Pascal's triangle]] |
||
=={{header|J}}== |
|||
In J, it's usually more natural to start counting from 0 rather than 1. That shows up subtly in this task, since the specified roots assume counting starts from 1. |
|||
Anyways: |
|||
<syntaxhighlight lang=J> tri=: [!+ |
|||
2 tri i. 30 |
|||
1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276 300 325 351 378 406 435 465 |
|||
3 tri i. 30 |
|||
1 4 10 20 35 56 84 120 165 220 286 364 455 560 680 816 969 1140 1330 1540 1771 2024 2300 2600 2925 3276 3654 4060 4495 4960 |
|||
4 tri i. 30 |
|||
1 5 15 35 70 126 210 330 495 715 1001 1365 1820 2380 3060 3876 4845 5985 7315 8855 10626 12650 14950 17550 20475 23751 27405 31465 35960 40920 |
|||
12 tri i. 30 |
|||
1 13 91 455 1820 6188 18564 50388 125970 293930 646646 1352078 2704156 5200300 9657700 17383860 30421755 51895935 86493225 141120525 225792840 354817320 548354040 834451800 1251677700 1852482996 2707475148 3910797436 5586853480 7898654920 |
|||
</syntaxhighlight> |
|||
And, for the roots: |
|||
<syntaxhighlight lang=J> r2=: 2 %~ _1 + 2 %: 1 8&p. |
|||
r3=: _1 + 0 3&p. (+ +&(3%:]) -) 2 %: _1r27 0 9&p. |
|||
r4=: 2 %~ _3 + 2 %: 5 + 4 * 2 %: 1 + 24 * ] |
|||
(r2,r3,r4) 7140 |
|||
119 34 18.8766 |
|||
(r2,r3,r4) 21408696 |
|||
6543 503.564 149.061 |
|||
(r2,r3,r4) 26728085384 |
|||
231205 5432 893.442 |
|||
(r2,r3,r4) 14545501785001 |
|||
5.39361e6 44356.2 4321 |
|||
</syntaxhighlight> |
|||