EKG sequence convergence: Difference between revisions

Added Go
(→‎{{header|Python}}: find_convergence as function)
(Added Go)
Line 25:
;Reference:
* [https://www.youtube.com/watch?v=yd2jr30K2R4 The EKG Sequence and the Tree of Numbers]. (Video).
 
=={{header|Go}}==
<lang go>package main
 
import "fmt"
 
const nPrimes = 15
 
var primes = [nPrimes]int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
 
func contains(a []int, b int) bool {
for _, j := range a {
if j == b {
return true
}
}
return false
}
 
func getState(used [nPrimes]bool) string {
var tf [15]byte
for i := 0; i < nPrimes; i++ {
if used[i] {
tf[i] = 't'
} else {
tf[i] = 'f'
}
}
return string(tf[:])
}
 
func main() {
const limit = 100
starts := [3]int{2, 5, 7}
var ekg [3][limit]int
var state [3][limit]string
 
for s, start := range starts {
ekg[s][0] = 1
ekg[s][1] = start
var used [nPrimes]bool
nextMember:
for n := 2; n < limit; n++ {
for i := 2; ; i++ {
// a potential sequence member cannot already have been used
if !contains(ekg[s][:n], i) {
for j, prime := range primes {
// must have a factor in common with previous member
if i%prime == 0 && ekg[s][n-1]%prime == 0 {
ekg[s][n] = i
used[j] = true
state[s][n] = getState(used)
continue nextMember
}
}
}
}
 
}
fmt.Printf("EKG(%d): %v\n", start, ekg[s][:10])
}
 
// now compare EKG(5) and EKG(7) for convergence
for i := 1; i < limit; i++ {
if ekg[1][i] == ekg[2][i] && state[1][i] == state[2][i] {
fmt.Println("\nEKG(5) and EKG(7) converge at term", i+1)
return
}
}
fmt.Println("\nEKG5(5) and EKG(7) do not converge within", limit, "terms")
}</lang>
 
{{out}}
<pre>
EKG(2): [1 2 4 6 3 9 12 8 10 5]
EKG(5): [1 5 10 2 4 6 3 9 12 8]
EKG(7): [1 7 14 2 4 6 3 9 12 8]
 
EKG(5) and EKG(7) converge at term 21
</pre>
 
=={{header|Python}}==
9,488

edits