Triangular numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Add summation of Teterahedrals to Pentatopics formulas, rearrange formulas a bit) |
(Added Go) |
||
Line 73: | Line 73: | ||
;* [[Pascal's_triangle|Related task: Pascal's triangle]] |
;* [[Pascal's_triangle|Related task: Pascal's triangle]] |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|Go-rcu}} |
|||
{{libheader|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. |
|||
<syntaxhighlight lang="go">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.Add(root, one) |
|||
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.Add(root, temp) |
|||
root = bigfloat.Pow(root, third) |
|||
root.Add(root, temp2) |
|||
root.Sub(root, one) |
|||
fmt.Printf("%14s: %.24f\n", "tetrahedral", root) |
|||
root.Mul(bx, twentyFour) |
|||
root.Add(root, one) |
|||
root.Sqrt(root) |
|||
root.Mul(root, four) |
|||
root.Add(root, five) |
|||
root.Sqrt(root) |
|||
root.Sub(root, three) |
|||
root.Quo(root, two) |
|||
fmt.Printf("%14s: %.24f\n", "pentatonic", root) |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |