Jump to content

Smallest power of 6 whose decimal expansion contains n: Difference between revisions

Added Go
(Added Algol W)
(Added Go)
Line 474:
20 170,581,728,179,578,208,256
21 216
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<lang go>package main
 
import (
"fmt"
"math/big"
"strconv"
"strings"
)
 
// Adds thousand separators to an integral string.
func commatize(s string) string {
neg := false
if strings.HasPrefix(s, "-") {
s = s[1:]
neg = true
}
le := len(s)
for i := le - 3; i >= 1; i -= 3 {
s = s[0:i] + "," + s[i:]
}
if !neg {
return s
}
return "-" + s
}
 
func main() {
fmt.Println(" n smallest power of 6 which contains n")
six := big.NewInt(6)
for n := 0; n <= 21; n++ {
ns := strconv.Itoa(n)
i := int64(0)
for {
bi := big.NewInt(i)
pow6 := bi.Exp(six, bi, nil).String()
if strings.Contains(pow6, ns) {
fmt.Printf("%2d 6^%-2d = %s\n", n, i, commatize(pow6))
break
}
i++
}
}
}</lang>
 
{{out}}
<pre>
n smallest power of 6 which contains n
0 6^9 = 10,077,696
1 6^0 = 1
2 6^3 = 216
3 6^2 = 36
4 6^6 = 46,656
5 6^6 = 46,656
6 6^1 = 6
7 6^5 = 7,776
8 6^12 = 2,176,782,336
9 6^4 = 1,296
10 6^9 = 10,077,696
11 6^16 = 2,821,109,907,456
12 6^4 = 1,296
13 6^13 = 13,060,694,016
14 6^28 = 6,140,942,214,464,815,497,216
15 6^18 = 101,559,956,668,416
16 6^3 = 216
17 6^10 = 60,466,176
18 6^15 = 470,184,984,576
19 6^21 = 21,936,950,640,377,856
20 6^26 = 170,581,728,179,578,208,256
21 6^3 = 216
</pre>
 
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.