Jump to content

Triangular numbers: Difference between revisions

m
→‎{{header|Raku}}: make rounding uniform across root formulas
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(1e-20): ε
}
 
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'
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.561826974636656182697463651404819613
pentatopic-root: 149.06094737526586748438757488471337060947375265867484387575
 
Roots of 26728085384:
triangular-root: 231205.405565255836957291031960694122305405565255836957291031961
tetrahedral-root: 5432
pentatopic-root: 893.44245675168486988846621152924537442456751684869888466212
 
Roots of 14545501785001:
triangular-root: 5393607.15814517231649730472465548460797158145172316497304724655
tetrahedral-root: 44355.777384073255777384073256052620916903
pentatopic-root: 4321</pre>
10,327

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.