Triangular numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|jq}}: explain `def figurates:`)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 6 users not shown)
Line 638:
'''Also works with gojq and fq, the Go implementations of jq'''
 
InThe main point of interest in the following, `$ris |probably `figurates/0`, which generates
an indefinitely long stream of the $r-simplex numbers if $r >= 2, where $r is the input to the filter.
For the sake of illustration, however, `tetrahedrals` and `pentatopics` are defined without reference to `figurates/0`.
 
'''Preliminaries'''
Line 694 ⟶ 695:
 
def tetrahedralRoot:
def cubrt: pow(.; 1/3);
def term(sign):
(3 * .) as $y
| ($y + sign * ( (($y*$y) - (1/27))|sqrt)) | cubrtcbrt;
term(1) + term(-1) -1;
 
Line 919:
tetrahedral-root: 44355.777384073255
pentatopic-root: 4321.0
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{trans|Julia}}
<syntaxhighlight lang="Mathematica">
(* Polytopic number generation function *)
Polytopic[r_, range_] := Binomial[# + r - 1, r] & /@ range
 
(* Triangular root function *)
TriangularRoot[x_] := (Sqrt[8 x + 1] - 1)/2
 
(* Tetrahedral root function *)
TetrahedralRoot[x_] := N[((3 x + Sqrt[9 x^2 - 1/27])^(1/3) +
(3 x - Sqrt[9 x^2 - 1/27])^(1/3) - 1), 18]
 
(* Pentatopic root function *)
PentatopicRoot[x_] := (Sqrt[5 + 4 Sqrt[24 x + 1]] - 3)/2
 
(* Displaying polytopic numbers *)
Do[
name = Which[
r == 2, "triangular",
r == 3, "tetrahedral",
r == 4, "pentatopic",
r == 12, "12-simplex"
];
Print["\nFirst 30 ", name, " numbers:\n", Polytopic[r, Range[0, 29]]],
{r,{2,3,4,12}}
]
 
(* Displaying roots of specific numbers *)
nums = {7140, 21408696, 26728085384, 14545501785001};
For[i = 1, i <= Length[nums], i++,
n = nums[[i]];
Print["\nRoots of ", n, ":"];
Print[" triangular-root: ", N@TriangularRoot[n]];
Print[" tetrahedral-root: ", N@TetrahedralRoot[n]];
Print[" pentatopic-root: ", N@PentatopicRoot[n]]
]
</syntaxhighlight>
{{out}}
<pre>
 
First 30 triangular numbers:
{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}
 
First 30 tetrahedral numbers:
{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}
 
First 30 pentatopic numbers:
{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}
 
First 30 12-simplex numbers:
{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}
 
Roots of 7140:
triangular-root: 119.
tetrahedral-root: 34.
pentatopic-root: 18.876646615928006
 
Roots of 21408696:
triangular-root: 6543.
tetrahedral-root: 503.5618269746365
pentatopic-root: 149.06094737526587
 
Roots of 26728085384:
triangular-root: 231205.40556525585
tetrahedral-root: 5432.
pentatopic-root: 893.4424567516849
 
Roots of 14545501785001:
triangular-root: 5.3936071581451725*^6
tetrahedral-root: 44355.777384073255
pentatopic-root: 4321.
 
</pre>
 
 
=={{header|Nim}}==
As described in the task presentation, we start the sequences at index 1.
<syntaxhighlight lang="Nim">import std/[math, strformat, strutils]
 
 
proc printNSimplexNumbers(r, count, width: Positive; title: string) =
## Print the first "count" terms of the "r-simplex" sequence
## using "width" characters.
echo title
for n in 1..count:
stdout.write align($binom(n + r - 1, r), width)
stdout.write if n mod 5 == 0: '\n' else: ' '
echo()
 
printNSimplexNumbers(2, 30, 3, "First 30 triangular numbers:")
printNSimplexNumbers(3, 30, 4, "First 30 tetrahedral numbers:")
printNSimplexNumbers(4, 30, 5, "First 30 pentatopic numbers:")
printNSimplexNumbers(12, 30, 10, "First 30 12-simplex numbers:")
 
 
func triangularRoot(x: float): float =
## Return the triangular root of "x".
(sqrt(8 * x + 1) - 1) * 0.5
 
func tetrahedralRoot(x: float): float =
## Return the tetrahedral root of "x".
let t1 = 3 * x
let t2 = sqrt(t1 * t1 - 1 / 27)
result = cbrt(t1 + t2) + cbrt(t1 - t2) - 1
 
func pentatopicRoot(x: float): float =
## Return the pentatopic root of "x".
(sqrt(5 + 4 * sqrt(24 * x + 1)) - 3) * 0.5
 
for n in [int64 7140, 21408696, 26728085384, 14545501785001]:
echo &"Roots of {n}:"
for (title, f) in {"triangular: ": triangularRoot,
"tetrahedral:": tetrahedralRoot,
"pentatopic: ": pentatopicRoot}:
echo &" {title} {f(n.float):.6f}"
echo()
</syntaxhighlight>
 
{{out}}
<pre>First 30 triangular numbers:
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
 
First 30 tetrahedral numbers:
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
 
First 30 pentatopic numbers:
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
 
First 30 12-simplex numbers:
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
 
Roots of 7140:
triangular: 119.000000
tetrahedral: 34.000000
pentatopic: 18.876647
 
Roots of 21408696:
triangular: 6543.000000
tetrahedral: 503.561166
pentatopic: 149.060947
 
Roots of 26728085384:
triangular: 231205.405565
tetrahedral: 5431.999939
pentatopic: 893.442457
 
Roots of 14545501785001:
triangular: 5393607.158145
tetrahedral: 44355.777377
pentatopic: 4321.000000
</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
/* Polytopic number generation function */
polytopic(r, range) = {
vector(#range, i, binomial(range[i] + r - 1, r))
}
 
/* Triangular root function */
triangularRoot(x) = {
(sqrt(8*x + 1) - 1)/2
}
 
/* Tetrahedral root function */
tetrahedralRoot(x) = {
N = (3*x + sqrt(9*x^2 - 1/27))^(1/3) + (3*x - sqrt(9*x^2 - 1/27))^(1/3) - 1;
precision(N, 18)
}
 
/* Pentatopic root function */
pentatopicRoot(x) = {
(sqrt(5 + 4*sqrt(24*x + 1)) - 3)/2
}
 
 
{
/* Displaying polytopic numbers */
r_sel=[2,3,4,12];
for(i = 1, #r_sel,
r=r_sel[i];
casename="place_holder";
if(r == 2, casename="triangular"; ,
r == 3, casename="tetrahedral"; ,
r == 4, casename="pentatopic"; ,
r == 12, casename="12-simplex";
);
printf("\nFirst 30 %s numbers:\n %s\n" , Str(casename) , Str(polytopic(r, [0..29])) )
);
 
/* Displaying roots of specific numbers */
nums = [7140, 21408696, 26728085384, 14545501785001];
for(i = 1, #nums,
n = nums[i];
printf("\nRoots of %s:\n", Str(n));
printf(" triangular-root: %s\n", Str(triangularRoot(n)));
printf(" tetrahedral-root: %s\n", Str(tetrahedralRoot(n)));
printf(" pentatopic-root: %s\n", Str(pentatopicRoot(n)))
);
}
</syntaxhighlight>
{{out}}
<pre>
 
First 30 triangular numbers:
[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]
 
First 30 tetrahedral numbers:
[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]
 
First 30 pentatopic numbers:
[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]
 
First 30 12-simplex numbers:
[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]
 
Roots of 7140:
triangular-root: 119.00000000000000000000000000000000000
tetrahedral-root: 34.00000000000000000
pentatopic-root: 18.876646615928006607901782826667566229
 
Roots of 21408696:
triangular-root: 6543.0000000000000000000000000000000000
tetrahedral-root: 503.5618269746365141
pentatopic-root: 149.06094737526586748438757488471336807
 
Roots of 26728085384:
triangular-root: 231205.40556525583695729103196069412230
tetrahedral-root: 5432.000000000000000
pentatopic-root: 893.44245675168486988846621152924537039
 
Roots of 14545501785001:
triangular-root: 5393607.1581451723164973047246554846080
tetrahedral-root: 44355.77738407325605
pentatopic-root: 4321.0000000000000000000000000000000000
 
</pre>
 
Line 1,088 ⟶ 1,348:
tetrahedral-root : 44355.777376558433
pentatopic -root : 4321.000000000000
</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl" line>
use v5.36;
use experimental <builtin for_list>;
use Math::AnyNum <binomial cbrt max round>;
 
sub table { my $t = 6 * (my $c = 1 + length max @_); ( sprintf( ('%'.$c.'d')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }
 
sub triangular_root ($x) {
round( (sqrt(8 * $x + 1) - 1) / 2, -3);
}
 
sub tetrahedral_root ($x) {
round(
cbrt(3 * $x + sqrt 9 * $x**2 - 1/27) +
cbrt(3 * $x - sqrt 9 * $x**2 - 1/27) - 1,
-3)
}
 
sub pentatopic_root ($x) {
round( (sqrt(5 + 4 * sqrt 24 * $x + 1) - 3) / 2, -3)
}
 
sub polytopic ($r, @range) { map { binomial $_ + $r - 1, $r } @range }
 
for my($r,$label) (2, 'triangular', 3, 'tetrahedral', 4, 'pentatopic', 12, '12-simplex') {
say "First 30 $label numbers:\n" . table polytopic $r, 0..29
}
 
for (7140, 21408696, 26728085384, 14545501785001) {
printf "Roots of $_:
triangular-root: %s
tetrahedral-root: %s
pentatopic-root: %s\n\n",
triangular_root($_), tetrahedral_root($_), pentatopic_root($_);
}
</syntaxhighlight>
{{out}}
<pre>
First 30 triangular numbers:
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
 
First 30 tetrahedral numbers:
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
 
First 30 pentatopic numbers:
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
 
First 30 12-simplex numbers:
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
 
Roots of 7140:
triangular-root: 119
tetrahedral-root: 34
pentatopic-root: 18.877
 
Roots of 21408696:
triangular-root: 6543
tetrahedral-root: 503.561
pentatopic-root: 149.061
 
Roots of 26728085384:
triangular-root: 231205.406
tetrahedral-root: 5432
pentatopic-root: 893.442
 
Roots of 14545501785001:
triangular-root: 5393607.158
tetrahedral-root: 44355.777
pentatopic-root: 4321
</pre>
 
Line 1,170 ⟶ 1,519:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>use Math::Root:ver<0.0.4>;
 
my \ε = FatRat.new: 1, 10**24;
 
sub binomial { [×] ($^n … 0) Z/ 1 .. $^p }
 
sub polytopic (Int $r, @range) { @range.map: { binomial $_ + $r - 1, $r } }
 
sub triangular-root ($x) { round ((8 × $x + 1).&root - 1) / 2, ε }
 
sub tetrahedral-root ($x) {
((3 × $x + (9 × $x² - 1/27).&root).&root(3) +
(3 × $x - (9 × $x² - 1/27).&root).&root(3) - 1).round: ε
}
 
sub pentatopic-root ($x) { round ((5 + 4 × (24 × $x + 1).&root).&root - 3) / 2, ε }
 
sub display (@values) {
Line 1,196 ⟶ 1,534:
 
say '';
 
my \ε = FatRat.new: 1, 10**24;
 
for 7140, 21408696, 26728085384, 14545501785001 {
say qq:to/R/;
Roots of $_:
triangular-root: {.&triangular-root.round: ε}
tetrahedral-root: {.&tetrahedral-root.round: ε}
pentatopic-root: {.&pentatopic-root.round: ε}
R
}</syntaxhighlight>
Line 1,295 ⟶ 1,635:
2: { 231205.405565 5431.99993865 893.442456752 }
1: { 5393607.15814 44355.7773766 4321 }
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func pentatopic_root(x) {
(sqrt(5 + 4*sqrt(24*x + 1)) - 3)/2
}
 
func polytopic (r, range) {
range.map {|n| binomial(n + r - 1, r) }
}
 
[
2, 'triangular', 3, 'tetrahedral', 4, 'pentatopic', 12, '12-simplex'
].slices(2).each_2d {|r,label|
say "\nFirst 30 #{label} numbers:"
polytopic(r, ^30).slices(6).each{.join(' ').say}
}
 
for n in (7140, 21408696, 26728085384, 14545501785001) {
printf ("\nRoots of #{n}:
triangular-root: %s
tetrahedral-root: %s
pentatopic-root: %s\n",
polygonal_root(n,3), pyramidal_root(n,3), pentatopic_root(n))
}</syntaxhighlight>
{{out}}
<pre>
First 30 triangular numbers:
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
 
First 30 tetrahedral numbers:
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
 
First 30 pentatopic numbers:
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
 
First 30 12-simplex numbers:
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
 
Roots of 7140:
triangular-root: 119
tetrahedral-root: 34
pentatopic-root: 18.8766466159280066079017828266675662291603339398
 
Roots of 21408696:
triangular-root: 6543
tetrahedral-root: 503.56182697463651404819613028417773405650502954
pentatopic-root: 149.060947375265867484387574884713368069543117436
 
Roots of 26728085384:
triangular-root: 231205.405565255836957291031960694122304324644392
tetrahedral-root: 5432
pentatopic-root: 893.442456751684869888466211529245370387840101701
 
Roots of 14545501785001:
triangular-root: 5393607.1581451723164973047246554846079685622181
tetrahedral-root: 44355.7773840732560526209168894228874431786835756
pentatopic-root: 4321
</pre>
 
Line 1,300 ⟶ 1,714:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./big" for BigRat
 
9,476

edits