Yellowstone sequence: Difference between revisions

Added Go
(Moved task, extra etc. out of contents list and rejigged them a bit.)
(Added Go)
Line 36:
:*   Applegate et al, 2015: The Yellowstone Permutation [https://arxiv.org/abs/1501.01669].
 
 
=={{header|Go}}==
This uses Gnuplot-X11 to do the plotting rather than a third party Go plotting library.
<lang go>package main
 
import (
"fmt"
"log"
"os/exec"
)
 
func gcd(x, y int) int {
for y != 0 {
x, y = y, x%y
}
return x
}
 
func yellowstone(n int) []int {
m := make(map[int]bool)
a := make([]int, n+1)
for i := 1; i < 4; i++ {
a[i] = i
m[i] = true
}
min := 3
for c := 4; c <= n; c++ {
for i := min + 1; ; i++ {
if !m[i] && gcd(a[c-1], i) == 1 && gcd(a[c-2], i) > 1 {
a[c] = i
m[i] = true
if i < min {
min = i
}
break
}
}
}
return a[1:]
}
 
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
 
func main() {
x := make([]int, 100)
for i := 0; i < 100; i++ {
x[i] = i + 1
}
y := yellowstone(100)
fmt.Println("The first 30 Yellowstone numbers are:")
fmt.Println(y[:30])
g := exec.Command("gnuplot", "-persist")
w, err := g.StdinPipe()
check(err)
check(g.Start())
fmt.Fprintln(w, "unset key; plot '-'")
for i, xi := range x {
fmt.Fprintf(w, "%d %d\n", xi, y[i])
}
fmt.Fprintln(w, "e")
w.Close()
g.Wait()
}</lang>
 
{{out}}
<pre>
The first 30 Yellowstone numbers are:
[1 2 3 4 9 8 15 14 5 6 25 12 35 16 7 10 21 20 27 22 39 11 13 33 26 45 28 51 32 17]
</pre>
 
=={{header|Julia}}==
9,490

edits