Triangular numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: Math::Root now includes the r-simplex root routines)
Line 1,170: Line 1,170:


=={{header|Raku}}==
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>use Math::Root;
<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 binomial { [×] ($^n … 0) Z/ 1 .. $^p }


sub polytopic (Int $r, @range) { @range.map: { binomial $_ + $r - 1, $r } }
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) {
sub display (@values) {
Line 1,196: Line 1,185:


say '';
say '';

my \ε = FatRat.new: 1, 10**24;


for 7140, 21408696, 26728085384, 14545501785001 {
for 7140, 21408696, 26728085384, 14545501785001 {
say qq:to/R/;
say qq:to/R/;
Roots of $_:
Roots of $_:
triangular-root: {.&triangular-root}
triangular-root: {.&triangular-root.round: ε}
tetrahedral-root: {.&tetrahedral-root}
tetrahedral-root: {.&tetrahedral-root.round: ε}
pentatopic-root: {.&pentatopic-root}
pentatopic-root: {.&pentatopic-root.round: ε}
R
R
}</syntaxhighlight>
}</syntaxhighlight>