Triangular numbers: Difference between revisions
m
→{{header|Raku}}: make rounding uniform across root formulas
Thundergnat (talk | contribs) m (→{{header|Raku}}: typo) |
Thundergnat (talk | contribs) m (→{{header|Raku}}: make rounding uniform across root formulas) |
||
Line 74:
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>use Math::Root;
my \ε = FatRat.new: 1, 10**24;
sub binomial { [×] ($^n … 0) Z/ 1 .. $^p }
Line 79 ⟶ 81:
sub polytopic (Int $r, @range) { @range.map: { binomial $_ + $r - 1, $r } }
sub triangular-root ($x) { (((8 × $x + 1).&root - 1) / 2).round: ε }
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) { (((5 + 4 × (24 × $x + 1).&root).&root - 3) / 2).round: ε }
sub display (@values) {
Line 93 ⟶ 95:
}
for 2, 'triangular', 3, 'tetrahedral',
4, 'pentatopic', 12, '12-simplex'
-> $r, $name {
say "\nFirst 30 $name numbers:\n" ~
Line 140 ⟶ 143:
Roots of 21408696:
triangular-root: 6543
tetrahedral-root: 503.
pentatopic-root: 149.
Roots of 26728085384:
triangular-root: 231205.
tetrahedral-root: 5432
pentatopic-root: 893.
Roots of 14545501785001:
triangular-root: 5393607.
tetrahedral-root: 44355.
pentatopic-root: 4321</pre>
|