Triangular numbers: Difference between revisions

Line 632:
5.39361e6 44356.2 4321
</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
 
'''Also works with gojq and fq, the Go implementations of jq'''
 
'''Preliminaries'''
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Display a stream of items in Z-style, n per line
def neatly(s; $n; $width):
def p: lpad($width);
foreach s as $x ({n: 1, s:""};
if .n >= $n
then .emit = .s + " " + ($x|p)
| .s = null
| .n = 1
else .emit = null
| .s = .s + " " + ($x|p)
| .n += 1
end;
select(.emit).emit);
 
# nCk assuming n >= k
def binomial(n; k):
if k > n / 2 then binomial(n; n-k)
else reduce range(1; k+1) as $i (1; . * (n - $i + 1) / $i)
end;
</syntaxhighlight>
<syntaxhighlight lang=jq>
def figurate($r; $n): binomial($n + $r -1; $r);
 
def triangular: binomial(.+1;2);
 
# r=2
def triangulars: foreach range(0; infinite) as $i (0; . + $i);
 
# r=3
def tetrahedrals: foreach triangulars as $t (0; . + $t);
 
# r=4
def pentatopics: foreach tetrahedrals as $t (0; . + $t);
 
# input: r
def figurates:
. as $r
| if $r == 2 then triangulars
else foreach ($r - 1 |figurates) as $t (0; . + $t)
end;
# r=12
def twelveSimplexes: 12 | figurates;
 
### r-simplex roots
 
def triangularRoot: ((8*. + 1 | sqrt) -1) /2;
 
def tetrahedralRoot:
def cubrt: pow(.; 1/3);
def term(sign):
(3 * .) as $y
| ($y + sign * ( (($y*$y) - (1/27))|sqrt)) | cubrt;
term(1) + term(-1) -1;
 
def pentatopicRoot:
(((5 + 4 * (( 24*. + 1)|sqrt)) | sqrt) - 3) / 2;
 
def xs: [7140, 21408696, 26728085384, 14545501785001];
 
def tasks:
def round($ndec): pow(10;$ndec) as $p | . * $p | round / $p;
def r: round(4) | lpad(12);
def s(stream): limit(30; neatly(stream; 5; 8));
"The first 30 triangular numbers are:", s(triangulars),
"\nThe first 30 tetrahedral numbers are:", s(tetrahedrals),
"\nThe first 30 pentatopic numbers are:", s(pentatopics),
"\nThe first 30 12-simplex numbers are:", neatly(limit(30; twelveSimplexes); 5; 12),
"",
"Approximate r-simplex roots:",
"\("x "|lpad(15)) triangularRoot tetrahedralRoot pentatopicRoot",
(xs[]
| "\(lpad(15)): \(triangularRoot|r) \(tetrahedralRoot|r) \(pentatopicRoot|r)")
 
;
 
tasks
</syntaxhighlight>
{{output}}
<pre>
The first 30 triangular numbers are:
0 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 496 528 561 595
630 666 703 741 780
820 861 903 946 990
1035 1081 1128 1176 1225
1275 1326 1378 1431 1485
1540 1596 1653 1711 1770
1830 1891 1953 2016 2080
2145 2211 2278 2346 2415
2485 2556 2628 2701 2775
2850 2926 3003 3081 3160
3240 3321 3403 3486 3570
3655 3741 3828 3916 4005
4095 4186 4278 4371 4465
4560 4656 4753 4851 4950
5050 5151 5253 5356 5460
5565 5671 5778 5886 5995
6105 6216 6328 6441 6555
6670 6786 6903 7021 7140
7260 7381 7503 7626 7750
7875 8001 8128 8256 8385
8515 8646 8778 8911 9045
9180 9316 9453 9591 9730
9870 10011 10153 10296 10440
10585 10731 10878 11026 11175
 
The first 30 tetrahedral numbers are:
0 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 5456 5984 6545 7140
7770 8436 9139 9880 10660
11480 12341 13244 14190 15180
16215 17296 18424 19600 20825
22100 23426 24804 26235 27720
29260 30856 32509 34220 35990
37820 39711 41664 43680 45760
47905 50116 52394 54740 57155
59640 62196 64824 67525 70300
73150 76076 79079 82160 85320
88560 91881 95284 98770 102340
105995 109736 113564 117480 121485
125580 129766 134044 138415 142880
147440 152096 156849 161700 166650
171700 176851 182104 187460 192920
198485 204156 209934 215820 221815
227920 234136 240464 246905 253460
260130 266916 273819 280840 287980
295240 302621 310124 317750 325500
333375 341376 349504 357760 366145
374660 383306 392084 400995 410040
419220 428536 437989 447580 457310
467180 477191 487344 497640 508080
518665 529396 540274 551300 562475
 
The first 30 pentatopic numbers are:
0 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 46376 52360 58905 66045
73815 82251 91390 101270 111930
123410 135751 148995 163185 178365
194580 211876 230300 249900 270725
292825 316251 341055 367290 395010
424270 455126 487635 521855 557845
595665 635376 677040 720720 766480
814385 864501 916895 971635 1028790
1088430 1150626 1215450 1282975 1353275
1426425 1502501 1581580 1663740 1749060
1837620 1929501 2024785 2123555 2225895
2331890 2441626 2555190 2672670 2794155
2919735 3049501 3183545 3321960 3464840
3612280 3764376 3921225 4082925 4249575
4421275 4598126 4780230 4967690 5160610
5359095 5563251 5773185 5989005 6210820
6438740 6672876 6913340 7160245 7413705
7673835 7940751 8214570 8495410 8783390
9078630 9381251 9691375 10009125 10334625
10668000 11009376 11358880 11716640 12082785
12457445 12840751 13232835 13633830 14043870
14463090 14891626 15329615 15777195 16234505
16701685 17178876 17666220 18163860 18671940
19190605 19720001 20260275 20811575 21374050
 
The first 30 12-simplex numbers are:
0 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
 
Approximate r-simplex roots:
x triangularRoot tetrahedralRoot pentatopicRoot
7140: 119 34 18.8766
21408696: 6543 503.5612 149.0609
26728085384: 231205.4056 5431.9999 893.4425
14545501785001: 5393607.1581 44355.7774 4321
</pre>
 
 
=={{header|Julia}}==
2,442

edits