Taxicab numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Go}}: User new and more efficient strings.Builder, remove timings)
Line 1,026: Line 1,026:
"container/heap"
"container/heap"
"fmt"
"fmt"
"strings"
)
)


Line 1,083: Line 1,084:


func (t Taxicab) String() string {
func (t Taxicab) String() string {
var b strings.Builder
//return fmt.Sprintf("%12d =%5d³ +%5d³ =%5d³ +%5d³",
// t[0].value, t[0].x, t[0].y, t[1].x, t[1].y)
fmt.Fprintf(&b, "%12d", t[0].value)

// Likely more efficient to use a bytes.Buffer here.
s := fmt.Sprintf("%12d", t[0].value)
for _, p := range t {
for _, p := range t {
s = s + fmt.Sprintf(" =%5d³ +%5d³", p.x, p.y)
fmt.Fprintf(&b, " =%5d³ +%5d³", p.x, p.y)
}
}
return s
return b.String()
}
}


Line 1,123: Line 1,121:
)
)
var tg TaxicabGen
var tg TaxicabGen
firstn := 3 // To show the first tripple, quadruple, etc
firstn := 3 // To show the first triple, quadruple, etc
for i := 1; i <= high+6; i++ {
for i := 1; i <= high+6; i++ {
t := tg.Next()
t := tg.Next()
Line 1,136: Line 1,134:
}
}
}</lang>
}</lang>
On a modern 64 bit processor this takes ~89 msec for the first 2000 and ~9.2 sec for the first 40,000.
{{out}}
{{out}}
<pre>
<pre>