Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
70
Task/Fusc-sequence/Go/fusc-sequence.go
Normal file
70
Task/Fusc-sequence/Go/fusc-sequence.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func fusc(n int) []int {
|
||||
if n <= 0 {
|
||||
return []int{}
|
||||
}
|
||||
if n == 1 {
|
||||
return []int{0}
|
||||
}
|
||||
res := make([]int, n)
|
||||
res[0] = 0
|
||||
res[1] = 1
|
||||
for i := 2; i < n; i++ {
|
||||
if i%2 == 0 {
|
||||
res[i] = res[i/2]
|
||||
} else {
|
||||
res[i] = res[(i-1)/2] + res[(i+1)/2]
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func fuscMaxLen(n int) [][2]int {
|
||||
maxLen := -1
|
||||
maxFusc := -1
|
||||
f := fusc(n)
|
||||
var res [][2]int
|
||||
for i := 0; i < n; i++ {
|
||||
if f[i] <= maxFusc {
|
||||
continue // avoid expensive strconv operation where possible
|
||||
}
|
||||
maxFusc = f[i]
|
||||
le := len(strconv.Itoa(f[i]))
|
||||
if le > maxLen {
|
||||
res = append(res, [2]int{i, f[i]})
|
||||
maxLen = le
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func commatize(n int) string {
|
||||
s := fmt.Sprintf("%d", n)
|
||||
if n < 0 {
|
||||
s = s[1:]
|
||||
}
|
||||
le := len(s)
|
||||
for i := le - 3; i >= 1; i -= 3 {
|
||||
s = s[0:i] + "," + s[i:]
|
||||
}
|
||||
if n >= 0 {
|
||||
return s
|
||||
}
|
||||
return "-" + s
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("The first 61 fusc numbers are:")
|
||||
fmt.Println(fusc(61))
|
||||
fmt.Println("\nThe fusc numbers whose length > any previous fusc number length are:")
|
||||
res := fuscMaxLen(20000000) // examine first twenty million numbers say
|
||||
for i := 0; i < len(res); i++ {
|
||||
fmt.Printf("%7s (index %10s)\n", commatize(res[i][1]), commatize(res[i][0]))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue