# Triangular numbers

Triangular numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A triangular number is a count of objects arranged into an equilateral triangle. Much like how a square number is a count of objects arranged into a square.

The nth triangular number is the sum of the first n non-negative integers. ${\displaystyle T_{n}=\sum _{k=1}^{n}k=1+2+3+\dotsb +n}$

Triangular numbers may be calculated by the explicit formulas: ${\displaystyle T_{n}={\frac {n(n+1)}{2}}={n+1 \choose 2}}$

where ${\displaystyle \textstyle {n+1 \choose 2}}$ is the binomial coefficient "n plus one choose two".

Analogous to square roots, we may also calculate a triangular root. Numbers that have an integer triangular root are triangular numbers.

The real triangular root of a number x may be found using: ${\displaystyle n={\frac {{\sqrt {8x+1}}-1}{2}}}$

Similar to how cubic numbers are square numbers extended into a third dimension, triangular numbers extended into a third dimension are known as tetrahedral numbers.

The nth tetrahedral number is the sum of the first n triangular numbers. ${\displaystyle Te_{n}=\sum _{k=1}^{n}T_{k}}$

Or, may be calculated directly: ${\displaystyle Te_{n}={\frac {n(n+1)(n+2)}{6}}={\frac {n^{\overline {3}}}{3!}}={\binom {n+2}{3}}}$ (Binomial "n plus two choose three".)

One may find the real tetrahedral root of x using the formula: ${\displaystyle n={\sqrt[{3}]{3x+{\sqrt {9{x^{2}}-{\frac {1}{27}}}}}}+{\sqrt[{3}]{3x-{\sqrt {9{x^{2}}-{\frac {1}{27}}}}}}-1}$
Depending on the math precision of your particular language, may need to be rounded to the nearest 1e-16 or so.

Extending into a fourth dimension we get pentatopic numbers.

Again, the nth pentatope is the sum of the first n tetrahedral numbers, ${\displaystyle P_{n}=\sum _{k=1}^{n}Te_{k}}$

or explicitly: ${\displaystyle P_{n}={\frac {n(n+1)(n+2)(n+3)}{24}}={\binom {n+3}{4}}}$ (Binomial "n plus three choose four".)

The pentatopic real root of x may be found using: ${\displaystyle n={\frac {{\sqrt {5+4{\sqrt {24x+1}}}}-3}{2}}.}$

In general, these all belong to the class figurate numbers as they are based on r dimensional geometric figures. Sometimes they are referred to as r-simplex numbers. In geometry a simplex is the simplest possible r-dimensional object.

You may easily extend to an arbitrary dimension r using binomials. Each term n in dimension r is ${\displaystyle r_{n}={\binom {n+r-1}{r}}}$

There is no known general formula to find higher r-simplex roots.

• Find and display the first 30 triangular numbers (r = 2).
• Find and display the first 30 tetrahedral numbers (r = 3).
• Find and display the first 30 pentatopic numbers (r = 4).
• Find and display the first 30 12-simplex numbers (r = 12).
• Find and display the triangular root, the tetrahedral root, and the pentatopic root for the integers:
• 7140
• 21408696
• 26728085384
• 14545501785001

## ALGOL 68

Assumes LONG INT is at least 64 bits.
Some roots that should be integers have non-integral values when calculated by the formulae in the task. To get the correct values (if possible), the root procedures used here calculate float value and then use the nearest integer or nearest integer + 1 if they are exact.

BEGIN # show some triangular, tetrahedral, ... numbers and roots             #
# prints a row of LONG INTs, with a title, newlines after every per-line #
# and each number in the specified width                                 #
PROC show values = ( STRING title, []LONG INT v, INT per line, INT width )VOID:
BEGIN
print( ( title, ":", newline ) );
INT on line := 0;
FOR i FROM LWB v TO UPB v DO
print( ( whole( v[ i ], -width ) ) );
IF ( on line +:= 1 ) = per line THEN
print( ( newline ) );
on line := 0
FI
OD;
IF on line /= 0 THEN print( ( newline ) ) FI;
print( ( newline ) )
END # show values # ;

# calculate the first 30 triangular, tetrahedral, etc. numbers           #
[ 1 : 30 ]LONG INT triangular;  triangular[  1 ] := 0;
[ 1 : 30 ]LONG INT tetrahedral; tetrahedral[ 1 ] := 0;
[ 1 : 30 ]LONG INT pentatopic;  pentatopic[  1 ] := 0;
[ 1 : 30 ]LONG INT simplex12;   simplex12[   1 ] := 0;
FOR i FROM 2 TO 30 DO
triangular[  i ] := triangular[  i - 1 ] + i - 1;
tetrahedral[ i ] := tetrahedral[ i - 1 ] + triangular[  i ];
pentatopic[  i ] := pentatopic[  i - 1 ] + tetrahedral[ i ];
simplex12[   i ] := pentatopic[  i     ]
OD;
FROM 5 TO 12 DO
FOR i FROM 2 TO 30 DO
simplex12[ i ] +:= simplex12[ i - 1 ]
OD
OD;
show values( "First 30 Triangular  numbers", triangular,  6,  4 );
show values( "First 30 Tetrahedral numbers", tetrahedral, 6,  5 );
show values( "First 30 Pentatopic  numbers", pentatopic,  6,  6 );
show values( "First 30 12-Simplex  numbers", simplex12,   6, 12 );

# show some triangular, tetrahedral, etc. roots                          #

# returns the cube root of x                                             #
PROC long crt = ( LONG REAL x )LONG REAL: long exp( long ln( x ) / 3 );
# returns a LONG REAL approximation to the  triangular root of x         #
PROC  real triangular root = ( LONG INT x )LONG REAL: ( long sqrt( ( 8 * x ) + 1 ) - 1 ) / 2;
# returns a LONG REAL approximation to the tetrahedral root of x         #
PROC real tetrahedral root = ( LONG INT x )LONG REAL:
BEGIN
LONG REAL t = long sqrt( ( 9 * x * x ) - ( 1 / 27 ) );
long crt( ( 3 * x ) + t ) + long crt( ( 3 * x ) - t ) - 1
END # tetrahedral root # ;
# returns a LONG REAL approximation to the  pentatopic root of x         #
PROC  real pentatopic root = ( LONG INT x )LONG REAL:
( long sqrt( 5 + ( 4 * long sqrt( ( 24 * x ) + 1 ) ) ) - 3 ) / 2;
# returns an integer root of x, if the approximation (possibly + 1 ) = x #
#                               the approximation otherwise              #
PROC try integer root = ( LONG INT x, LONG REAL real root, PROC( LONG INT )LONG INT f )LONG REAL:
IF LONG INT ir = ENTIER real root;
f( ir ) = x
THEN ir
ELIF f( ir + 1 ) = x
THEN ir + 1
ELSE real root
FI # try integer root # ;
# returns the  triangular root of x                                      #
PROC  triangular root = ( LONG INT x )LONG REAL:
try integer root( x
, real triangular root( x )
, ( LONG INT n )LONG INT: ( n * ( n + 1 ) ) OVER 2
);
# returns the tetrahedral root of x                                      #
PROC tetrahedral root = ( LONG INT x )LONG REAL:
try integer root( x
, real tetrahedral root( x )
, ( LONG INT n )LONG INT: ( n * ( n + 1 ) * ( n + 2 ) ) OVER 6
);
# returns the  pentatopic root of x                                      #
PROC  pentatopic root = ( LONG INT x )LONG REAL:
try integer root( x
, real pentatopic root( x )
, ( LONG INT n )LONG INT: ( n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) ) OVER 24
);
[]LONG INT root test = ( 7140, 21408696, 26728085384, 14545501785001 );
FOR i FROM LWB root test TO UPB root test DO
PROC show = ( LONG REAL x )STRING:
IF ENTIER x = x THEN whole( x, -6 ) + "      " ELSE fixed( x, -12, 5 ) FI;
print( ( "Roots of ", whole( root test[ i ], 0 ), newline, "   " ) );
print( ( " triangular: ",  show(  triangular root( root test[ i ] ) ) ) );
print( ( " tetrahedral: ", show( tetrahedral root( root test[ i ] ) ) ) );
print( ( " pentatopic: ",  show(  pentatopic root( root test[ i ] ) ) ) );
print( ( newline ) )
OD
END
Output:
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:    119       tetrahedral:     34       pentatopic:     18.87665
Roots of 21408696
triangular:   6543       tetrahedral:    503.56183 pentatopic:    149.06095
Roots of 26728085384
triangular: 231205.40557 tetrahedral:   5432       pentatopic:    893.44246
Roots of 14545501785001
triangular: 5393607.1581 tetrahedral:  44355.77738 pentatopic:   4321


## AppleScript

on rSimplexNumber(r, n)
set n to n - 1 -- "nth" is 0-based in the formula!
set numerator to n
set denominator to 1
repeat with dimension from 2 to r
set numerator to numerator * (n + dimension - 1)
set denominator to denominator * dimension
end repeat

return numerator div denominator
end rSimplexNumber

on triangularRoot(x)
return ((8 * x + 1) ^ 0.5 - 1) / 2
end triangularRoot

on tetrahedralRoot(x)
-- NOT (((9 * (x ^ 2) - 1 / 27) ^ 0.5 + 3 * x) ^ (1 / 3)) * 2 - 1  !
return (((9 * (x ^ 2) - 1 / 27) ^ 0.5 + 3 * x) ^ (1 / 3)) - 1
end tetrahedralRoot

on pentatopicRoot(x)
return (((24 * x + 1) ^ 0.5 * 4 + 5) ^ 0.5 - 3) / 2
end pentatopicRoot

on intToText(int)
set txt to ""
repeat while (int > 99999999)
set txt to ((100000000 + int mod 100000000) as integer as text)'s text 2 thru 9 & txt
set int to int div 100000000
end repeat
return (int as text) & txt
end intToText

on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join

set output to {}
set columnWidth to (count intToText(rSimplexNumber(12, 30))) + 2
repeat with rt in {{2, "triangular"}, {3, "tetrahedral"}, {5, "pentatopic"}, {12, "12-simplex"}}
set {r, type} to rt
set end of output to linefeed & "First thirty " & type & " numbers:"
set these6 to {}
repeat with n from 1 to 30
set this to intToText(rSimplexNumber(r, n))
set these6's end to (padding & this)'s text -columnWidth thru -1
if (n mod 6 = 0) then
set end of output to join(these6, "")
set these6 to {}
end if
end repeat
end repeat
repeat with n in {7140, 21408696, 2.6728085384E+10, 1.4545501785001E+13}
set end of output to linefeed & "Roots of " & intToText(n) & ":"
set end of output to "  Triangular root: " & triangularRoot(n)
set end of output to "  Tetrahedral root: " & tetrahedralRoot(n)
set end of output to "  Pentatopic root: " & pentatopicRoot(n)
end repeat
return join(output, linefeed)


Output:
"
First thirty 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 thirty 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 thirty pentatopic numbers:
0           1           6          21          56         126
252         462         792        1287        2002        3003
4368        6188        8568       11628       15504       20349
26334       33649       42504       53130       65780       80730
98280      118755      142506      169911      201376      237336

First thirty 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.0
Tetrahedral root: 33.990473597552
Pentatopic root: 18.876646615928

Roots of 21408696:
Triangular root: 6543.0
Tetrahedral root: 503.561166334548
Pentatopic root: 149.060947375266

Roots of 26728085384:
Triangular root: 2.312054055653E+5
Tetrahedral root: 5431.99993864654
Pentatopic root: 893.442456751685

Roots of 14545501785001:
Triangular root: 5.393607158145E+6
Tetrahedral root: 4.435577737656E+4
Pentatopic root: 4321.0"


## FreeBASIC

Translation of: Wren
Dim As Integer n, r, t(0 To 30)
t(0) = 0

Print "The first 30 triangular numbers are:"
For n = 1 To 30
t(n) = t(n-1) + n - 1
If n Mod 6 = 0 Then Print Using "####"; t(n) Else Print Using "####"; t(n);
Next n

Print !"\nThe first 30 tetrahedral numbers are:"
For n = 1 To 30
t(n) += t(n-1)
Print Using "#####"; t(n);
If n Mod 6 = 0 Then Print
Next n

Print !"\nThe first 30 pentatopic numbers are:"
For n = 1 To 30
t(n) += t(n-1)
Print Using "######"; t(n);
If n Mod 6 = 0 Then Print
Next n

Print !"\nThe first 30 12-simplex numbers are:"
For r = 5 To 12
For n = 1 To 30
t(n) += t(n-1)
If r = 12 Then
Print Using "###########"; t(n);
If n Mod 6 = 0 Then Print
End If
Next n
Next r

#define cRec27 1/sqr(27)
Dim As Integer xs(1 To 4) = {7140, 21408696, 26728085384, 14545501785001}
Dim As Double x, y, z

For i As Byte = 1 To 4
z = xs(i)
Print !"\nRoots of"; xs(i); ":"
Print " triangular:"; (Sqr(8*z+1)-1)/2

y = 3*z
x = Sqr((y-cRec27)*(y+cRec27))
Print "tetrahedral:"; Iif(x < y, Exp(Log(y+x)/3)+Exp(Log(y-x)/3)-1, Exp(Log(6)/3)*Exp(Log(z)/3)-1)

Print " pentatopic:"; (Sqr(5+4*Sqr(24*z+1))-3)/2
Next i
Sleep
Output:
The first 30 triangular numbers are:
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

The first 30 tetrahedral numbers are:
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

The first 30 pentatopic numbers are:
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

The first 30 12-simplex numbers are:
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: 119
tetrahedral: 34.00000000179027
pentatopic: 18.87664661592801

Roots of 21408696:
triangular: 6543
tetrahedral: 503.5611663345483
pentatopic: 149.0609473752659

Roots of 26728085384:
triangular: 231205.4055652559
tetrahedral: 5431.99993864654
pentatopic: 893.4424567516849

Roots of 14545501785001:
triangular: 5393607.158145173
tetrahedral: 44355.77737655847
pentatopic: 4321

## Go

Translation of: Wren
Library: Go-rcu
Library: bigfloat

I've had to use a third party library to calculate cube roots as the big.Float type in the standard library doesn't have a function for this. The results (to 24 d.p) are the same as the Raku example with the exception of the tetrahedral root for the largest integer which differs in the last three places.

package main

import (
"fmt"
"github.com/ALTree/bigfloat"
"math/big"
"rcu"
)

func main() {
t := make([]int, 30)
for n := 1; n < 30; n++ {
t[n] = t[n-1] + n
}
fmt.Println("The first 30 triangular numbers are:")
rcu.PrintTable(t, 6, 3, false)

for n := 1; n < 30; n++ {
t[n] += t[n-1]
}
fmt.Println("\nThe first 30 tetrahedral numbers are:")
rcu.PrintTable(t, 6, 4, false)

for n := 1; n < 30; n++ {
t[n] += t[n-1]
}
fmt.Println("\nThe first 30 pentatopic numbers are:")
rcu.PrintTable(t, 6, 5, false)

for r := 5; r <= 12; r++ {
for n := 1; n < 30; n++ {
t[n] += t[n-1]
}
}
fmt.Println("\nThe first 30 12-simplex numbers are:")
rcu.PrintTable(t, 6, 10, false)

const prec = 256
xs := []float64{7140, 21408696, 26728085384, 14545501785001}
root := new(big.Float)
temp := new(big.Float)
temp2 := new(big.Float)
one := big.NewFloat(1)
two := big.NewFloat(2)
three := big.NewFloat(3)
four := big.NewFloat(4)
five := big.NewFloat(5)
eight := big.NewFloat(8)
nine := big.NewFloat(9)
twentyFour := big.NewFloat(24)
twentySeven := big.NewFloat(27)
third := new(big.Float).SetPrec(prec).Quo(one, three)
for _, x := range xs {
bx := big.NewFloat(x).SetPrec(prec)
fmt.Printf("\nRoots of %d:\n", int(x))
root.Mul(bx, eight)
root.Sqrt(root)
root.Sub(root, one)
root.Quo(root, two)
fmt.Printf("%14s: %.24f\n", "triangular", root)

temp.Mul(bx, bx)
temp.Mul(temp, nine)
temp.Sub(temp, new(big.Float).SetPrec(prec).Quo(one, twentySeven))
temp.Sqrt(temp)
temp2.Mul(bx, three)
temp2.Sub(temp2, temp)
temp2 = bigfloat.Pow(temp2, third)
root.Mul(bx, three)
root = bigfloat.Pow(root, third)
root.Sub(root, one)
fmt.Printf("%14s: %.24f\n", "tetrahedral", root)

root.Mul(bx, twentyFour)
root.Sqrt(root)
root.Mul(root, four)
root.Sqrt(root)
root.Sub(root, three)
root.Quo(root, two)
fmt.Printf("%14s: %.24f\n", "pentatonic", root)
}
}

Output:
The first 30 triangular numbers are:
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

The first 30 tetrahedral numbers are:
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

The first 30 pentatopic numbers are:
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

The first 30 12-simplex numbers are:
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: 119.000000000000000000000000
tetrahedral: 34.000000000000000000000000
pentatonic: 18.876646615928006607901783

Roots of 21408696:
triangular: 6543.000000000000000000000000
tetrahedral: 503.561826974636514048196130
pentatonic: 149.060947375265867484387575

Roots of 26728085384:
triangular: 231205.405565255836957291031961
tetrahedral: 5432.000000000000000000000000
pentatonic: 893.442456751684869888466212

Roots of 14545501785001:
triangular: 5393607.158145172316497304724655
tetrahedral: 44355.777384073256052620916889
pentatonic: 4321.000000000000000000000000


## J

In J, it's usually more natural to start counting from 0 rather than 1. That shows up subtly in this task, since the specified roots assume counting starts from 1.

Anyways:

   tri=: [!+
2 tri 1+i.5 6
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 496
3 tri 1+i.5 6
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 5456
4 tri 1+i.5 6
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 46376
12 tri 1+i.10 3
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 11058116888


And, for the roots:

   r2=: 2 %~ _1 + 2 %: 1 8&p.
r3=: _1 + 0 3&p. (+ +&(3%:]) -) 2 %: _1r27 0 9&p.
r4=: 2 %~ _3 + 2 %: 5 + 4 * 2 %: 1 + 24 * ]
(r2,r3,r4) 7140
119 34 18.8766
(r2,r3,r4) 21408696
6543 503.564 149.061
(r2,r3,r4) 26728085384
231205 5432 893.442
(r2,r3,r4) 14545501785001
5.39361e6 44356.2 4321


## jq

Works with: jq

Also works with gojq and fq, the Go implementations of jq

The main point of interest in the following is 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 def lpad($len): tostring | ($len - length) as$l | (" " * $l)[:$l] + .;

# Display a stream of items in Z-style, n per line
def neatly(s; $n;$width):
def p: lpad($width); foreach s as$x ({n: 1, s:""};
if .n >= $n then .emit = .s + " " + ($x|p)
| .s = null
| .n = 1
else .emit = null
| .s = .s + " " + ($x|p) | .n += 1 end; select(.emit).emit); # nCk assuming n >= k def binomial(n; k): if k > n / 2 then binomial(n; n-k) else reduce range(1; k+1) as$i (1; . * (n - $i + 1) /$i)
end;
def figurate($r;$n): binomial($n +$r -1; $r); def triangular: binomial(.+1;2); # r=2 def triangulars: foreach range(0; infinite) as$i (0; . + $i); # r=3 def tetrahedrals: foreach triangulars as$t (0; . + $t); # r=4 def pentatopics: foreach tetrahedrals as$t (0; . + $t); # input: r def figurates: . as$r
| if $r == 2 then triangulars else foreach ($r - 1 |figurates) as $t (0; . +$t)
end;

# r=12
def twelveSimplexes: 12 | figurates;

###  r-simplex roots

def triangularRoot: ((8*. + 1 | sqrt) -1) /2;

def tetrahedralRoot:
def term(sign):
(3 * .) as $y | ($y + sign * ( (($y*$y) - (1/27))|sqrt)) | cbrt;
term(1) + term(-1) -1;

def pentatopicRoot:
(((5 + 4 * (( 24*. + 1)|sqrt)) | sqrt) - 3) / 2;

def xs: [7140, 21408696, 26728085384, 14545501785001];

def round($ndec): pow(10;$ndec) as $p | . *$p | round / $p; def r: round(4) | lpad(12); def s(stream): limit(30; neatly(stream; 5; 8)); "The first 30 triangular numbers are:", s(triangulars), "\nThe first 30 tetrahedral numbers are:", s(tetrahedrals), "\nThe first 30 pentatopic numbers are:", s(pentatopics), "\nThe first 30 12-simplex numbers are:", neatly(limit(30; twelveSimplexes); 5; 12), "", "Approximate r-simplex roots:", "\("x "|lpad(15)) triangularRoot tetrahedralRoot pentatopicRoot", (xs[] | "\(lpad(15)): \(triangularRoot|r) \(tetrahedralRoot|r) \(pentatopicRoot|r)") ; tasks Output: The first 30 triangular numbers are: 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 465 496 528 561 595 630 666 703 741 780 820 861 903 946 990 1035 1081 1128 1176 1225 1275 1326 1378 1431 1485 1540 1596 1653 1711 1770 1830 1891 1953 2016 2080 2145 2211 2278 2346 2415 2485 2556 2628 2701 2775 2850 2926 3003 3081 3160 3240 3321 3403 3486 3570 3655 3741 3828 3916 4005 4095 4186 4278 4371 4465 4560 4656 4753 4851 4950 5050 5151 5253 5356 5460 5565 5671 5778 5886 5995 6105 6216 6328 6441 6555 6670 6786 6903 7021 7140 7260 7381 7503 7626 7750 7875 8001 8128 8256 8385 8515 8646 8778 8911 9045 9180 9316 9453 9591 9730 9870 10011 10153 10296 10440 10585 10731 10878 11026 11175 The first 30 tetrahedral numbers are: 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 4960 5456 5984 6545 7140 7770 8436 9139 9880 10660 11480 12341 13244 14190 15180 16215 17296 18424 19600 20825 22100 23426 24804 26235 27720 29260 30856 32509 34220 35990 37820 39711 41664 43680 45760 47905 50116 52394 54740 57155 59640 62196 64824 67525 70300 73150 76076 79079 82160 85320 88560 91881 95284 98770 102340 105995 109736 113564 117480 121485 125580 129766 134044 138415 142880 147440 152096 156849 161700 166650 171700 176851 182104 187460 192920 198485 204156 209934 215820 221815 227920 234136 240464 246905 253460 260130 266916 273819 280840 287980 295240 302621 310124 317750 325500 333375 341376 349504 357760 366145 374660 383306 392084 400995 410040 419220 428536 437989 447580 457310 467180 477191 487344 497640 508080 518665 529396 540274 551300 562475 The first 30 pentatopic numbers are: 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 40920 46376 52360 58905 66045 73815 82251 91390 101270 111930 123410 135751 148995 163185 178365 194580 211876 230300 249900 270725 292825 316251 341055 367290 395010 424270 455126 487635 521855 557845 595665 635376 677040 720720 766480 814385 864501 916895 971635 1028790 1088430 1150626 1215450 1282975 1353275 1426425 1502501 1581580 1663740 1749060 1837620 1929501 2024785 2123555 2225895 2331890 2441626 2555190 2672670 2794155 2919735 3049501 3183545 3321960 3464840 3612280 3764376 3921225 4082925 4249575 4421275 4598126 4780230 4967690 5160610 5359095 5563251 5773185 5989005 6210820 6438740 6672876 6913340 7160245 7413705 7673835 7940751 8214570 8495410 8783390 9078630 9381251 9691375 10009125 10334625 10668000 11009376 11358880 11716640 12082785 12457445 12840751 13232835 13633830 14043870 14463090 14891626 15329615 15777195 16234505 16701685 17178876 17666220 18163860 18671940 19190605 19720001 20260275 20811575 21374050 The first 30 12-simplex numbers are: 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 Approximate r-simplex roots: x triangularRoot tetrahedralRoot pentatopicRoot 7140: 119 34 18.8766 21408696: 6543 503.5612 149.0609 26728085384: 231205.4056 5431.9999 893.4425 14545501785001: 5393607.1581 44355.7774 4321  ## Julia Translation of: Raku """ rosettacode.org task Triangular_numbers """ polytopic(r, range) = map(n -> binomial(n + r - 1, r), range) triangular_root(x) = (sqrt(8x + 1) - 1) / 2 function tetrahedral_root(x) return Float64(round((3x + sqrt(9 * x^big"2" - big"1"/27))^(big"1"/3) + (3x - sqrt(9 * x^big"2" - big"1"/27))^(big"1"/3) - 1, digits=18)) end pentatopic_root(x) = (sqrt(5 + 4 * sqrt(24x + 1)) - 3) / 2 function valuelisting(a, N=6) c = maximum(length, string.(a)) + 1 return join([join([lpad(x, c) for x in v]) for v in Iterators.partition(a, N)], "\n") end for (r, name) in [[2, "triangular"], [3, "tetrahedral"], [4, "pentatopic"], [12, "12-simplex"]] println("\nFirst 30$name numbers:\n", valuelisting(polytopic(r, 0:29)))
end

for n in [7140, 21408696, 26728085384, 14545501785001]
println("\nRoots of n:") println(" triangular-root: ", triangular_root(n)) println(" tetrahedral-root: ", tetrahedral_root(n)) println(" pentatopic-root: ", pentatopic_root(n)) end  Output: 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.0 tetrahedral-root: 34.0 pentatopic-root: 18.876646615928006 Roots of 21408696: triangular-root: 6543.0 tetrahedral-root: 503.5618269746365 pentatopic-root: 149.06094737526587 Roots of 26728085384: triangular-root: 231205.40556525585 tetrahedral-root: 5432.0 pentatopic-root: 893.4424567516849 Roots of 14545501785001: triangular-root: 5.3936071581451725e6 tetrahedral-root: 44355.777384073255 pentatopic-root: 4321.0  ## Mathematica/Wolfram Language Translation of: Julia (* 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]] ]  Output:  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.  ## Nim As described in the task presentation, we start the sequences at index 1. 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()

Output:
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


## PARI/GP

Translation of: Julia
/* 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)))
);
}
Output:

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



## Pascal

### Pascal

Using only extended isn't that precise for tetrahedral roots.
sqrt(sqr(3x)+1/27) is nearly 3x for bigger x values.

program XangularNumbers;
const
MAXIDX = 29;
MAXLINECNT = 13;
cNames : array[0..4] of string =
('','','triangular','tetrahedral','pentatopic');
cCheckRootValues :array[0..3] of Uint64 =
(7140,21408696,26728085384,14545501785001)   ;
type
tOneLine  = array[0..MAXIDX+2] of Uint64;
tpOneLine = ^tOneLine;
tSimplexs  = array[0..MAXLINECNT-1] of tOneLine;

procedure OutLine(var S:tSimplexs;idx: NativeInt);
const
cColCnt = 6;cColWidth = 80 DIV cColCnt;
var
i,colcnt : NativeInt;
begin
if idx > High(cNames) then
writeln('First ',MAXIDX+1,' ',idx,'-simplex numbers')
else
writeln('First ',MAXIDX+1,' ',cNames[idx],' numbers');
colcnt := cColCnt;
For i := 0 to MAXIDX do
begin
write(S[idx,i]:cColWidth);
dec(colCnt);
if ColCnt = 0 then
Begin
writeln;
ColCnt := cColCnt;
end;
end;
if ColCnt <  cColCnt then
writeln;
writeln;
end;

procedure CalcNextLine(var S:tSimplexs;idx: NativeInt);
var
s1,s2: Uint64;
i : NativeInt;
begin
s1 := S[idx,0];
S[idx+1,0] := s1;
For i := 1 to MAXIDX do
begin
s2:= S[idx,i];
S[idx+1,i] := s1+s2;
inc(s1,s2);
end;
end;

procedure InitSimplexs(var S:tSimplexs);
var
i: NativeInt;
begin
fillChar(S,Sizeof(S),#0);
For i := 1 to MAXIDX do
S[0,i] := 1;
For i := 0 to MAXLINECNT-2 do
CalcNextLine(S,i);
end;

function TriangularRoot(n: Uint64): extended;
begin
if n < High(Uint64) DIV 8 then
TriangularRoot := (sqrt(8*n+1)-1) / 2
else
TriangularRoot := (sqrt(8)*sqrt(n)-1)/2;
end;

function tetrahedralRoot(n: Uint64): extended;
const
cRec27 = 1/sqrt(27);
var
x,y : extended;
begin
y := 3.0*n;
x := sqrt((y-cRec27)*(y+cRec27));//sqrt(sqr(3*n)-1/27)
if x < y then
tetrahedralRoot := exp(ln(y+x)/3.0)+exp(ln(y-x)/3.0)-1.0
else
//( 6*n)^(1/3)-1
tetrahedralRoot :=exp(ln(6)/3.0)*exp(ln(n)/3.0)-1.0; //6^(1/3)* n^(1/3)-1
end;

function PentatopicRoot(n: Uint64): extended;
begin
PentatopicRoot := (sqrt(5 + 4 * sqrt(24*n + 1)) - 3) / 2;
end;

var
Simplexs  : tSimplexs;
n : Uint64;
i : NativeInt;
Begin
InitSimplexs(Simplexs);
OutLine(Simplexs,2);
OutLine(Simplexs,3);
OutLine(Simplexs,4);
OutLine(Simplexs,12);
For i := 0 to High(cCheckRootValues) do
begin
n := cCheckRootValues[i];
writeln('Roots of ',n,':');
writeln('triangular -root : ',TriangularRoot(n):20:12);
writeln('tetrahedral-root : ',tetrahedralRoot(n):20:12);
writeln('pentatopic -root : ',PentatopicRoot(n):20:12);
writeln;
end;
end.

Output:
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.000000000000
tetrahedral-root :      34.000000000003
pentatopic -root :      18.876646615928

Roots of 21408696:
triangular -root :    6543.000000000000
tetrahedral-root :     503.561826261328
pentatopic -root :     149.060947375266

Roots of 26728085384:
triangular -root :  231205.405565255837
tetrahedral-root :    5431.999938646542 <<==
pentatopic -root :     893.442456751685

Roots of 14545501785001:
triangular -root : 5393607.158145172316
tetrahedral-root :   44355.777376558433
pentatopic -root :    4321.000000000000


## Perl

Translation of: Raku
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($_); }  Output: 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  ## Phix with javascript_semantics sequence t = repeat(0,30) for n=2 to 30 do t[n] = t[n-1] + n-1 end for printf(1,"The first 30 triangular numbers are:\n%s\n",{join_by(t,1,6,fmt:="%3d")}) for n=2 to 30 do t[n] = t[n] + t[n-1] end for printf(1,"The first 30 tetrahedral numbers are:\n%s\n",{join_by(t,1,6,fmt:="%4d")}) for n=2 to 30 do t[n] = t[n] + t[n-1] end for printf(1,"The first 30 pentatopic numbers are:\n%s\n",{join_by(t,1,6,fmt:="%5d")}) for r=5 to 12 do for n=2 to 30 do t[n] = t[n] + t[n-1] end for end for printf(1,"The first 30 12-simplex numbers are:\n%s\n",{join_by(t,1,6,fmt:="%10d")}) for x in {7140, 21408696, 26728085384, 14545501785001} do printf(1,"\nRoots of %d:\n",x) atom root = (sqrt(x*8 + 1)-1)/2 printf(1,"%14s: %f\n", {"triangular", root}) atom temp = sqrt(x*x*9 - 1/27) root = power(x*3 + temp,1/3) + power(x*3 - temp,1/3) - 1 printf(1,"%14s: %f\n", {"tetrahedral", root}) root = (sqrt(sqrt(x*24 + 1)*4 + 5) - 3) / 2 printf(1,"%14s: %f\n", {"pentatopic", root}) end for  Output: The first 30 triangular numbers are: 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 The first 30 tetrahedral numbers are: 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 The first 30 pentatopic numbers are: 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 The first 30 12-simplex numbers are: 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: 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  ## Raku use Math::Root:ver<0.0.4>; sub binomial { [×] ($^n … 0) Z/ 1 .. $^p } sub polytopic (Int$r, @range) { @range.map: { binomial $_ +$r - 1, $r } } sub display (@values) { my$c = @values.max.chars;
@values.batch(6)».fmt("%{$c}d").join: "\n"; } for 2, 'triangular', 3, 'tetrahedral', 4, 'pentatopic', 12, '12-simplex' ->$r, $name { say "\nFirst 30$name numbers:\n" ~ display polytopic $r, ^30 } 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
}

Output:
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.876646615928006607901783

Roots of 21408696:
triangular-root: 6543
tetrahedral-root: 503.56182697463651404819613
pentatopic-root: 149.060947375265867484387575

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

Roots of 14545501785001:
triangular-root: 5393607.158145172316497304724655
tetrahedral-root: 44355.777384073256052620916903
pentatopic-root: 4321

## RPL

≪ DUP ROT + 1 - SWAP COMB
≫ 'SMPLX' STO

≪ 8 * 1 + √ 1 - 2 /
≫ 'TROOT' STO

≪ DUP SQ 9 * 27 INV - √ SWAP 3 *
DUP2 + 3 INV ^ SWAP ROT - 3 INV ^ + 1 -
≫ 'TeROOT' STO

≪ 24 * 1 + √ 4 * 5 + √ 3 - 2 /
≫ 'PROOT' STO

≪ {} 1 30 FOR n n 3 PICK SMPLX + NEXT
≫ 'SPX30' STO

≪ {7140 21408696 26728085384 14545501785001}
1 4 FOR n
DUP n GET {}
OVER TROOT + OVER TeROOT + SWAP PROOT + SWAP
NEXT DROP
≫ 'ROOTS' STO

Input:
2 SPX30
3 SPX30
4 SPX30
12 SPX30
ROOTS

Output:
8: { 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 }
7: { 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 }
6: { 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 }
5: { 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 }
4: { 119 34.0000000018 18.8766466159 }
3: { 6543 503.561166335 149.060947375 }
2: { 231205.405565 5431.99993865 893.442456752 }
1: { 5393607.15814 44355.7773766 4321 }


## Sidef

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))
}

Output:
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


## Wren

Library: Wren-fmt
Library: Wren-big
import "./fmt" for Fmt
import "./big" for BigRat

var t = List.filled(30, 0)
for (n in 1..29) t[n] = t[n-1] + n
System.print("The first 30 triangular numbers are:")
Fmt.tprint("$3d", t, 6) for (n in 1..29) t[n] = t[n] + t[n-1] System.print("\nThe first 30 tetrahedral numbers are:") Fmt.tprint("$4d", t, 6)

for (n in 1..29) t[n] = t[n] + t[n-1]
System.print("\nThe first 30 pentatopic numbers are:")
Fmt.tprint("$5d", t, 6) for (r in 5..12) { for (n in 1..29) t[n] = t[n] + t[n-1] } System.print("\nThe first 30 12-simplex numbers are:") Fmt.tprint("$10d", t, 6)

var xs = [7140, 21408696, 26728085384, 14545501785001]
var digs = 16
for (x in xs) {
var bx = BigRat.new(x)
System.print("\nRoots of %(x):")
var root = ((bx*8 + 1).sqrt(digs) - 1)/2
Fmt.print("$14s:$s", "triangular", root.toDecimal(digs-5))

var temp = (bx*bx*9 - BigRat.new(1, 27)).sqrt(digs)
root = (bx*3 + temp).cbrt(digs) + (bx*3 - temp).cbrt(digs) - 1
Fmt.print("$14s:$s", "tetrahedral", root.toDecimal(digs-5))

root = (((bx*24 + 1).sqrt(digs)*4 + 5).sqrt(digs) - 3) / 2
Fmt.print("$14s:$s", "pentatopic", root.toDecimal(digs-5))
}

Output:
The first 30 triangular numbers are:
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

The first 30 tetrahedral numbers are:
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

The first 30 pentatopic numbers are:
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

The first 30 12-simplex numbers are:
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: 119
tetrahedral: 34.00000000000
pentatopic: 18.87664661593

Roots of 21408696:
triangular: 6543
tetrahedral: 503.56182697464
pentatopic: 149.06094737527

Roots of 26728085384:
triangular: 231205.40556525584
tetrahedral: 5432.00000000000
pentatopic: 893.44245675168

Roots of 14545501785001:
triangular: 5393607.15814517232
tetrahedral: 44355.77738407326
pentatopic: 4321


## XPL0

Some "interesting" loss of precision in the Pow function....

real T(13, 30);

proc ShowRoots(X);
real X, SR, CR1, CR2;
[Format(1, 0);
Text(0, "Roots of ");  RlOut(0, X);  CrLf(0);
Format(7, 13);
Text(0, " triangular: ");
RlOut(0, (sqrt(8.*X + 1.) - 1.) / 2.);
Text(0, "^m^jtetrahedral: ");
SR:= sqrt(9.*X*X - 1./27.);
CR1:= Pow(3.*X + SR, 1./3.);
CR2:= Pow(3.*X - SR, 1./3.);
RlOut(0, CR1 + CR2 -1.);
Text(0, "^m^j pentatopic: ");
RlOut(0, (sqrt(5. + 4.*sqrt(24.*X + 1.)) - 3.) / 2.);
CrLf(0);  CrLf(0);
];

proc Print(Str, Places, R);
int  Str, Places, R, N;
[Text(0, Str);  CrLf(0);
Format(Places, 0);
for N:= 0 to 29 do
[RlOut(0, T(R,N));
if rem(N/6) = 5 then CrLf(0);
];
CrLf(0);
];

int  R, N;
[for N:= 0 to 29 do
T(1,N):= float(N);
for R:= 2 to 12 do
[T(R,0):= 0.;
for N:= 1 to 29 do
T(R,N):= T(R,N-1) + T(R-1,N);
];
Print("The first 30 triangular numbers are:", 4, 2);
Print("The first 30 tetrahedral numbers are:", 5, 3);
Print("The first 30 pentatopic numbers are:", 6, 4);
Print("The first 30 12-simplex numbers are:", 11, 12);
ShowRoots(7140.);
ShowRoots(21408696.);
ShowRoots(26728085384.);
ShowRoots(14_545_501_785_001.);
]
Output:
The first 30 triangular numbers are:
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

The first 30 tetrahedral numbers are:
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

The first 30 pentatopic numbers are:
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

The first 30 12-simplex numbers are:
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:     119.0000000000000
tetrahedral:      34.0000000017903
pentatopic:      18.8766466159280

Roots of 21408696
triangular:    6543.0000000000000
tetrahedral:     503.5611663345480
pentatopic:     149.0609473752660

Roots of 26728085384
triangular:  231205.4055652560000
tetrahedral:    5431.9999386465400
pentatopic:     893.4424567516850

Roots of 14545501785001
triangular: 5393607.1581451700000
tetrahedral:   44355.7773765584000
pentatopic:    4321.0000000000000