Triangular numbers: Difference between revisions
Content added Content deleted
(Add Mathematica/Wolfram Language implementation) |
(Add PARI/GP implementation) |
||
Line 1,092: | Line 1,092: | ||
tetrahedral: 44355.777377 |
tetrahedral: 44355.777377 |
||
pentatopic: 4321.000000 |
pentatopic: 4321.000000 |
||
</pre> |
|||
=={{header|PARI/GP}}== |
|||
{{trans|Julia}} |
|||
<syntaxhighlight lang="PARI/GP"> |
|||
/* 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))) |
|||
); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
</pre> |
||