Triangular numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: make rounding uniform across root formulas)
Line 74: Line 74:
=={{header|Raku}}==
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>use Math::Root;
<syntaxhighlight lang="raku" line>use Math::Root;

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


sub binomial { [×] ($^n … 0) Z/ 1 .. $^p }
sub binomial { [×] ($^n … 0) Z/ 1 .. $^p }
Line 79: Line 81:
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) { ((8 × $x + 1).&root - 1) / 2 }
sub triangular-root ($x) { (((8 × $x + 1).&root - 1) / 2).round: ε }


sub tetrahedral-root ($x) {
sub tetrahedral-root ($x) {
((3 × $x + (9 × $x² - 1/27).&root).&root(3) +
((3 × $x + (9 × $x² - 1/27).&root).&root(3) +
(3 × $x - (9 × $x² - 1/27).&root).&root(3) - 1).round(1e-20)
(3 × $x - (9 × $x² - 1/27).&root).&root(3) - 1).round: ε
}
}


sub pentatopic-root ($x) { ((5 + 4 × (24 × $x + 1).&root).&root - 3) / 2 }
sub pentatopic-root ($x) { (((5 + 4 × (24 × $x + 1).&root).&root - 3) / 2).round: ε }


sub display (@values) {
sub display (@values) {
Line 93: Line 95:
}
}


for 2, 'triangular', 3, 'tetrahedral', 4, 'pentatopic', 12, '12-simplex'
for 2, 'triangular', 3, 'tetrahedral',
4, 'pentatopic', 12, '12-simplex'
-> $r, $name {
-> $r, $name {
say "\nFirst 30 $name numbers:\n" ~
say "\nFirst 30 $name numbers:\n" ~
Line 140: Line 143:
Roots of 21408696:
Roots of 21408696:
triangular-root: 6543
triangular-root: 6543
tetrahedral-root: 503.5618269746366
tetrahedral-root: 503.56182697463651404819613
pentatopic-root: 149.06094737526586748438757488471337
pentatopic-root: 149.060947375265867484387575


Roots of 26728085384:
Roots of 26728085384:
triangular-root: 231205.405565255836957291031960694122305
triangular-root: 231205.405565255836957291031961
tetrahedral-root: 5432
tetrahedral-root: 5432
pentatopic-root: 893.44245675168486988846621152924537
pentatopic-root: 893.442456751684869888466212


Roots of 14545501785001:
Roots of 14545501785001:
triangular-root: 5393607.15814517231649730472465548460797
triangular-root: 5393607.158145172316497304724655
tetrahedral-root: 44355.777384073255
tetrahedral-root: 44355.777384073256052620916903
pentatopic-root: 4321</pre>
pentatopic-root: 4321</pre>