Triangular numbers: Difference between revisions

Added AppleScript.
(Added Go)
(Added AppleScript.)
Line 73:
;* [[Pascal's_triangle|Related task: Pascal's triangle]]
 
 
=={{header|AppleScript}}==
 
<syntaxhighlight lang="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
 
on task()
set output to {}
set padding 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)
end task
 
return task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"
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"</syntaxhighlight>
 
=={{header|Go}}==
557

edits