Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Executable-library/Go/executable-library-1.go
Normal file
37
Task/Executable-library/Go/executable-library-1.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// modulino.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Function borrowed from Hailstone sequence task.
|
||||
// 1st arg is the number to generate the sequence for.
|
||||
// 2nd arg is a slice to recycle, to reduce garbage.
|
||||
func hailstone(n int, recycle []int) []int {
|
||||
s := append(recycle[:0], n)
|
||||
for n > 1 {
|
||||
if n&1 == 0 {
|
||||
n = n / 2
|
||||
} else {
|
||||
n = 3*n + 1
|
||||
}
|
||||
s = append(s, n)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func libMain() {
|
||||
seq := hailstone(27, nil)
|
||||
fmt.Println("\nHailstone sequence for the number 27:")
|
||||
fmt.Println(" has", len(seq), "elements")
|
||||
fmt.Println(" starts with", seq[0:4])
|
||||
fmt.Println(" ends with", seq[len(seq)-4:])
|
||||
|
||||
var longest, length int
|
||||
for i := 1; i < 100000; i++ {
|
||||
if le := len(hailstone(i, nil)); le > length {
|
||||
longest = i
|
||||
length = le
|
||||
}
|
||||
}
|
||||
fmt.Printf("\n%d has the longest Hailstone sequence, its length being %d.\n", longest, length)
|
||||
}
|
||||
6
Task/Executable-library/Go/executable-library-2.go
Normal file
6
Task/Executable-library/Go/executable-library-2.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// modulino_main.go
|
||||
package main
|
||||
|
||||
func main() {
|
||||
libMain()
|
||||
}
|
||||
19
Task/Executable-library/Go/executable-library-3.go
Normal file
19
Task/Executable-library/Go/executable-library-3.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// hailstone.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
freq := make(map[int]int)
|
||||
for i := 1; i < 100000; i++ {
|
||||
freq[len(hailstone(i, nil))]++
|
||||
}
|
||||
var mk, mv int
|
||||
for k, v := range freq {
|
||||
if v > mv {
|
||||
mk = k
|
||||
mv = v
|
||||
}
|
||||
}
|
||||
fmt.Printf("\nThe Hailstone length returned most is %d, which occurs %d times.\n", mk, mv)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue