WeixiangYan/CodeTransOcean huggingface dataset:
Task: Welch's t-test




Go: package main

import (
  "fmt"
  "math"
)

var (
  d1 = []float64{27.5, 21.0, 19.0, 23.6, 17.0, 17.9, 16.9, 20.1, 21.9, 22.6,
    23.1, 19.6, 19.0, 21.7, 21.4}
  d2 = []float64{27.1, 22.0, 20.8, 23.4, 23.4, 23.5, 25.8, 22.0, 24.8, 20.2,
    21.9, 22.1, 22.9, 20.5, 24.4}
  d3 = []float64{17.2, 20.9, 22.6, 18.1, 21.7, 21.4, 23.5, 24.2, 14.7, 21.8}
  d4 = []float64{21.5, 22.8, 21.0, 23.0, 21.6, 23.6, 22.5, 20.7, 23.4, 21.8,
    20.7, 21.7, 21.5, 22.5, 23.6, 21.5, 22.5, 23.5, 21.5, 21.8}
  d5 = []float64{19.8, 20.4, 19.6, 17.8, 18.5, 18.9, 18.3, 18.9, 19.5, 22.0}
  d6 = []float64{28.2, 26.6, 20.1, 23.3, 25.2, 22.1, 17.7, 27.6, 20.6, 13.7,
    23.2, 17.5, 20.6, 18.0, 23.9, 21.6, 24.3, 20.4, 24.0, 13.2}
  d7 = []float64{30.02, 29.99, 30.11, 29.97, 30.01, 29.99}
  d8 = []float64{29.89, 29.93, 29.72, 29.98, 30.02, 29.98}
  x  = []float64{3.0, 4.0, 1.0, 2.1}
  y  = []float64{490.2, 340.0, 433.9}
)

func main() {
  fmt.Printf("%.6f\n", pValue(d1, d2))
  fmt.Printf("%.6f\n", pValue(d3, d4))
  fmt.Printf("%.6f\n", pValue(d5, d6))
  fmt.Printf("%.6f\n", pValue(d7, d8))
  fmt.Printf("%.6f\n", pValue(x, y))
}

func mean(a []float64) float64 {
  sum := 0.
  for _, x := range a {
    sum += x
  }
  return sum / float64(len(a))
}

func sv(a []float64) float64 {
  m := mean(a)
  sum := 0.
  for _, x := range a {
    d := x - m
    sum += d * d
  }
  return sum / float64(len(a)-1)
}

func welch(a, b []float64) float64 {
  return (mean(a) - mean(b)) /
    math.Sqrt(sv(a)/float64(len(a))+sv(b)/float64(len(b)))
}

func dof(a, b []float64) float64 {
  sva := sv(a)
  svb := sv(b)
  n := sva/float64(len(a)) + svb/float64(len(b))
  return n * n /
    (sva*sva/float64(len(a)*len(a)*(len(a)-1)) +
      svb*svb/float64(len(b)*len(b)*(len(b)-1)))
}

func simpson0(n int, upper float64, f func(float64) float64) float64 {
  sum := 0.
  nf := float64(n)
  dx0 := upper / nf
  sum += f(0) * dx0
  sum += f(dx0*.5) * dx0 * 4
  x0 := dx0
  for i := 1; i < n; i++ {
    x1 := float64(i+1) * upper / nf
    xmid := (x0 + x1) * .5
    dx := x1 - x0
    sum += f(x0) * dx * 2
    sum += f(xmid) * dx * 4
    x0 = x1
  }
  return (sum + f(upper)*dx0) / 6
}

func pValue(a, b []float64) float64 {
  ν := dof(a, b)
  t := welch(a, b)
  g1, _ := math.Lgamma(ν / 2)
  g2, _ := math.Lgamma(.5)
  g3, _ := math.Lgamma(ν/2 + .5)
  return simpson0(2000, ν/(t*t+ν),
    func(r float64) float64 { return math.Pow(r, ν/2-1) / math.Sqrt(1-r) }) /
    math.Exp(g1+g2-g3)
}
Task: Mayan calendar




Go: package main

import (
    "fmt"
    "strconv"
    "strings"
    "time"
)

var sacred = strings.Fields("Imix’ Ik’ Ak’bal K’an Chikchan Kimi Manik’ Lamat Muluk Ok Chuwen Eb Ben Hix Men K’ib’ Kaban Etz’nab’ Kawak Ajaw")

var civil = strings.Fields("Pop Wo’ Sip Sotz’ Sek Xul Yaxk’in Mol Ch’en Yax Sak’ Keh Mak K’ank’in Muwan’ Pax K’ayab Kumk’u Wayeb’")

var (
    date1 = time.Date(2012, time.December, 21, 0, 0, 0, 0, time.UTC)
    date2 = time.Date(2019, time.April, 2, 0, 0, 0, 0, time.UTC)
)

func tzolkin(date time.Time) (int, string) {
    diff := int(date.Sub(date1).Hours()) / 24
    rem := diff % 13
    if rem < 0 {
        rem = 13 + rem
    }
    var num int
    if rem <= 9 {
        num = rem + 4
    } else {
        num = rem - 9
    }
    rem = diff % 20
    if rem <= 0 {
        rem = 20 + rem
    }
    return num, sacred[rem-1]
}

func haab(date time.Time) (string, string) {
    diff := int(date.Sub(date2).Hours()) / 24
    rem := diff % 365
    if rem < 0 {
        rem = 365 + rem
    }
    month := civil[(rem+1)/20]
    last := 20
    if month == "Wayeb’" {
        last = 5
    }
    d := rem%20 + 1
    if d < last {
        return strconv.Itoa(d), month
    }
    return "Chum", month
}

func longCount(date time.Time) string {
    diff := int(date.Sub(date1).Hours()) / 24
    diff += 13 * 400 * 360
    baktun := diff / (400 * 360)
    diff %= 400 * 360
    katun := diff / (20 * 360)
    diff %= 20 * 360
    tun := diff / 360
    diff %= 360
    winal := diff / 20
    kin := diff % 20
    return fmt.Sprintf("%d.%d.%d.%d.%d", baktun, katun, tun, winal, kin)    
}

func lord(date time.Time) string {
    diff := int(date.Sub(date1).Hours()) / 24
    rem := diff % 9
    if rem <= 0 {
        rem = 9 + rem
    }
    return fmt.Sprintf("G%d", rem)
}

func main() {
    const shortForm = "2006-01-02"
    dates := []string{
        "2004-06-19",
        "2012-12-18",
        "2012-12-21",
        "2019-01-19",
        "2019-03-27",
        "2020-02-29",
        "2020-03-01",
        "2071-05-16",
    }
    fmt.Println(" Gregorian   Tzolk’in        Haab’              Long           Lord of")
    fmt.Println("   Date       # Name       Day Month            Count         the Night")
    fmt.Println("----------   --------    -------------        --------------  ---------")
    for _, dt := range dates {
        date, _ := time.Parse(shortForm, dt)
        n, s := tzolkin(date)
        d, m := haab(date)
        lc := longCount(date)
        l := lord(date)
        fmt.Printf("%s   %2d %-8s %4s %-9s       %-14s     %s\n", dt, n, s, d, m, lc, l)
    }
}
Task: Sorting algorithms_Tree sort on a linked list




Go: package main

import (
    "container/list"
    "fmt"
)

type BinaryTree struct {
    node         int
    leftSubTree  *BinaryTree
    rightSubTree *BinaryTree
}

func (bt *BinaryTree) insert(item int) {
    if bt.node == 0 {
        bt.node = item
        bt.leftSubTree = &BinaryTree{}
        bt.rightSubTree = &BinaryTree{}
    } else if item < bt.node {
        bt.leftSubTree.insert(item)
    } else {
        bt.rightSubTree.insert(item)
    }
}

func (bt *BinaryTree) inOrder(ll *list.List) {
    if bt.node == 0 {
        return
    }
    bt.leftSubTree.inOrder(ll)
    ll.PushBack(bt.node)
    bt.rightSubTree.inOrder(ll)
}
func treeSort(ll *list.List) *list.List {
    searchTree := &BinaryTree{}
    for e := ll.Front(); e != nil; e = e.Next() {
        i := e.Value.(int)
        searchTree.insert(i)
    }
    ll2 := list.New()
    searchTree.inOrder(ll2)
    return ll2
}

func printLinkedList(ll *list.List, f string, sorted bool) {
    for e := ll.Front(); e != nil; e = e.Next() {
        i := e.Value.(int)
        fmt.Printf(f+" ", i)
    }
    if !sorted {
        fmt.Print("-> ")
    } else {
        fmt.Println()
    }
}

func main() {
    sl := []int{5, 3, 7, 9, 1}
    ll := list.New()
    for _, i := range sl {
        ll.PushBack(i)
    }
    printLinkedList(ll, "%d", false)
    lls := treeSort(ll)
    printLinkedList(lls, "%d", true)

    sl2 := []int{'d', 'c', 'e', 'b', 'a'}
    ll2 := list.New()
    for _, c := range sl2 {
        ll2.PushBack(c)
    }
    printLinkedList(ll2, "%c", false)
    lls2 := treeSort(ll2)
    printLinkedList(lls2, "%c", true)
}
Task: Four is the number of letters in the ...




Go: package main

import (
	"fmt"
	"strings"
	"unicode"
)

func main() {
	f := NewFourIsSeq()
	fmt.Print("The lengths of the first 201 words are:")
	for i := 1; i <= 201; i++ {
		if i%25 == 1 {
			fmt.Printf("\n%3d: ", i)
		}
		_, n := f.WordLen(i)
		fmt.Printf(" %2d", n)
	}
	fmt.Println()
	fmt.Println("Length of sentence so far:", f.TotalLength())
	
	for i := 1000; i <= 1e7; i *= 10 {
		w, n := f.WordLen(i)
		fmt.Printf("Word %8d is %q, with %d letters.", i, w, n)
		fmt.Println("  Length of sentence so far:", f.TotalLength())
	}
}

type FourIsSeq struct {
	i     int      
	words []string 
}

func NewFourIsSeq() *FourIsSeq {
	return &FourIsSeq{
		
		words: []string{
			"Four", "is", "the", "number",
			"of", "letters", "in", "the",
			"first", "word", "of", "this", "sentence,",
		},
	}
}


func (f *FourIsSeq) WordLen(w int) (string, int) {
	for len(f.words) < w {
		f.i++
		n := countLetters(f.words[f.i])
		ns := say(int64(n))
		os := sayOrdinal(int64(f.i+1)) + ","
		
		f.words = append(f.words, strings.Fields(ns)...)
		f.words = append(f.words, "in", "the")
		f.words = append(f.words, strings.Fields(os)...)
	}
	word := f.words[w-1]
	return word, countLetters(word)
}



func (f FourIsSeq) TotalLength() int {
	cnt := 0
	for _, w := range f.words {
		cnt += len(w) + 1
	}
	return cnt - 1
}

func countLetters(s string) int {
	cnt := 0
	for _, r := range s {
		if unicode.IsLetter(r) {
			cnt++
		}
	}
	return cnt
}
Task: Wordle comparison




Go: package main

import (
    "bytes"
    "fmt"
    "log"
)

func wordle(answer, guess string) []int {
    n := len(guess)
    if n != len(answer) {
        log.Fatal("The words must be of the same length.")
    }
    answerBytes := []byte(answer)
    result := make([]int, n) 
    for i := 0; i < n; i++ {
        if guess[i] == answerBytes[i] {
            answerBytes[i] = '\000'
            result[i] = 2
        }
    }
    for i := 0; i < n; i++ {
        ix := bytes.IndexByte(answerBytes, guess[i])
        if ix >= 0 {
            answerBytes[ix] = '\000'
            result[i] = 1
        }
    }
    return result
}

func main() {
    colors := []string{"grey", "yellow", "green"}
    pairs := [][]string{
        {"ALLOW", "LOLLY"},
        {"BULLY", "LOLLY"},
        {"ROBIN", "ALERT"},
        {"ROBIN", "SONIC"},
        {"ROBIN", "ROBIN"},
    }
    for _, pair := range pairs {
        res := wordle(pair[0], pair[1])
        res2 := make([]string, len(res))
        for i := 0; i < len(res); i++ {
            res2[i] = colors[res[i]]
        }
        fmt.Printf("%s v %s => %v => %v\n", pair[0], pair[1], res, res2)
    }
}
Task: Minimal steps down to 1




Go: package main

import (
    "fmt"
    "strings"
)

const limit = 50000

var (
    divs, subs []int
    mins       [][]string
)


func minsteps(n int) {
    if n == 1 {
        mins[1] = []string{}
        return
    }
    min := limit
    var p, q int
    var op byte
    for _, div := range divs {
        if n%div == 0 {
            d := n / div
            steps := len(mins[d]) + 1
            if steps < min {
                min = steps
                p, q, op = d, div, '/'
            }
        }
    }
    for _, sub := range subs {
        if d := n - sub; d >= 1 {
            steps := len(mins[d]) + 1
            if steps < min {
                min = steps
                p, q, op = d, sub, '-'
            }
        }
    }
    mins[n] = append(mins[n], fmt.Sprintf("%c%d -> %d", op, q, p))
    mins[n] = append(mins[n], mins[p]...)
}

func main() {
    for r := 0; r < 2; r++ {
        divs = []int{2, 3}
        if r == 0 {
            subs = []int{1}
        } else {
            subs = []int{2}
        }
        mins = make([][]string, limit+1)
        fmt.Printf("With: Divisors: %v, Subtractors: %v =>\n", divs, subs)
        fmt.Println("  Minimum number of steps to diminish the following numbers down to 1 is:")
        for i := 1; i <= limit; i++ {
            minsteps(i)
            if i <= 10 {
                steps := len(mins[i])
                plural := "s"
                if steps == 1 {
                    plural = " "
                }
                fmt.Printf("    %2d: %d step%s: %s\n", i, steps, plural, strings.Join(mins[i], ", "))
            }
        }
        for _, lim := range []int{2000, 20000, 50000} {
            max := 0
            for _, min := range mins[0 : lim+1] {
                m := len(min)
                if m > max {
                    max = m
                }
            }
            var maxs []int
            for i, min := range mins[0 : lim+1] {
                if len(min) == max {
                    maxs = append(maxs, i)
                }
            }
            nums := len(maxs)
            verb, verb2, plural := "are", "have", "s"
            if nums == 1 {
                verb, verb2, plural = "is", "has", ""
            }
            fmt.Printf("  There %s %d number%s in the range 1-%d ", verb, nums, plural, lim)
            fmt.Printf("that %s maximum 'minimal steps' of %d:\n", verb2, max)
            fmt.Println("   ", maxs)
        }
        fmt.Println()
    }
}
Task: Continued fraction_Arithmetic_G(matrix ng, continued fraction n1, continued fraction n2)




Go: package cf

import "math"



















type NG8 struct {
	A12, A1, A2, A int64
	B12, B1, B2, B int64
}


var (
	NG8Add = NG8{0, 1, 1, 0, 0, 0, 0, 1}
	NG8Sub = NG8{0, 1, -1, 0, 0, 0, 0, 1}
	NG8Mul = NG8{1, 0, 0, 0, 0, 0, 0, 1}
	NG8Div = NG8{0, 1, 0, 0, 0, 0, 1, 0}
)

func (ng *NG8) needsIngest() bool {
	if ng.B12 == 0 || ng.B1 == 0 || ng.B2 == 0 || ng.B == 0 {
		return true
	}
	x := ng.A / ng.B
	return ng.A1/ng.B1 != x || ng.A2/ng.B2 != x && ng.A12/ng.B12 != x
}

func (ng *NG8) isDone() bool {
	return ng.B12 == 0 && ng.B1 == 0 && ng.B2 == 0 && ng.B == 0
}

func (ng *NG8) ingestWhich() bool { 
	if ng.B == 0 && ng.B2 == 0 {
		return true
	}
	if ng.B == 0 || ng.B2 == 0 {
		return false
	}
	x1 := float64(ng.A1) / float64(ng.B1)
	x2 := float64(ng.A2) / float64(ng.B2)
	x := float64(ng.A) / float64(ng.B)
	return math.Abs(x1-x) > math.Abs(x2-x)
}

func (ng *NG8) ingest(isN1 bool, t int64) {
	if isN1 {
		
		
		ng.A12, ng.A1, ng.A2, ng.A,
			ng.B12, ng.B1, ng.B2, ng.B =
			ng.A2+ng.A12*t, ng.A+ng.A1*t, ng.A12, ng.A1,
			ng.B2+ng.B12*t, ng.B+ng.B1*t, ng.B12, ng.B1
	} else {
		
		
		ng.A12, ng.A1, ng.A2, ng.A,
			ng.B12, ng.B1, ng.B2, ng.B =
			ng.A1+ng.A12*t, ng.A12, ng.A+ng.A2*t, ng.A2,
			ng.B1+ng.B12*t, ng.B12, ng.B+ng.B2*t, ng.B2
	}
}

func (ng *NG8) ingestInfinite(isN1 bool) {
	if isN1 {
		
		
		ng.A2, ng.A, ng.B2, ng.B =
			ng.A12, ng.A1,
			ng.B12, ng.B1
	} else {
		
		
		ng.A1, ng.A, ng.B1, ng.B =
			ng.A12, ng.A2,
			ng.B12, ng.B2
	}
}

func (ng *NG8) egest(t int64) {
	
	
	ng.A12, ng.A1, ng.A2, ng.A,
		ng.B12, ng.B1, ng.B2, ng.B =
		ng.B12, ng.B1, ng.B2, ng.B,
		ng.A12-ng.B12*t, ng.A1-ng.B1*t, ng.A2-ng.B2*t, ng.A-ng.B*t
}





func (ng NG8) ApplyTo(N1, N2 ContinuedFraction, limit int) ContinuedFraction {
	return func() NextFn {
		next1, next2 := N1(), N2()
		done := false
		sinceEgest := 0
		return func() (int64, bool) {
			if done {
				return 0, false
			}
			for ng.needsIngest() {
				sinceEgest++
				if sinceEgest > limit {
					done = true
					return 0, false
				}
				isN1 := ng.ingestWhich()
				next := next2
				if isN1 {
					next = next1
				}
				if t, ok := next(); ok {
					ng.ingest(isN1, t)
				} else {
					ng.ingestInfinite(isN1)
				}
			}
			sinceEgest = 0
			t := ng.A / ng.B
			ng.egest(t)
			done = ng.isDone()
			return t, true
		}
	}
}
Task: Ramanujan's constant




Go: package main

import (
    "fmt"
    "github.com/ALTree/bigfloat"
    "math/big"
)

const (
    prec = 256 
    ps   = "3.1415926535897932384626433832795028841971693993751058209749445923078164"
)

func q(d int64) *big.Float {
    pi, _ := new(big.Float).SetPrec(prec).SetString(ps)
    t := new(big.Float).SetPrec(prec).SetInt64(d)
    t.Sqrt(t)
    t.Mul(pi, t)
    return bigfloat.Exp(t)
}

func main() {
    fmt.Println("Ramanujan's constant to 32 decimal places is:")
    fmt.Printf("%.32f\n", q(163))
    heegners := [4][2]int64{
        {19, 96},
        {43, 960},
        {67, 5280},
        {163, 640320},
    }
    fmt.Println("\nHeegner numbers yielding 'almost' integers:")
    t := new(big.Float).SetPrec(prec)
    for _, h := range heegners {
        qh := q(h[0])
        c := h[1]*h[1]*h[1] + 744
        t.SetInt64(c)
        t.Sub(t, qh)
        fmt.Printf("%3d: %51.32f ≈ %18d (diff: %.32f)\n", h[0], qh, c, t)
    }
}
Task: Tree from nesting levels




Go: package main

import "fmt"

type any = interface{}

func toTree(list []int) any {
    s := []any{[]any{}}
    for _, n := range list {
        for n != len(s) {
            if n > len(s) {
                inner := []any{}
                s[len(s)-1] = append(s[len(s)-1].([]any), inner)
                s = append(s, inner)
            } else {
                s = s[0 : len(s)-1]
            }
        }
        s[len(s)-1] = append(s[len(s)-1].([]any), n)
        for i := len(s) - 2; i >= 0; i-- {
            le := len(s[i].([]any))
            s[i].([]any)[le-1] = s[i+1]
        }
    }
    return s[0]
}

func main() {
    tests := [][]int{
        {},
        {1, 2, 4},
        {3, 1, 3, 1},
        {1, 2, 3, 1},
        {3, 2, 1, 3},
        {3, 3, 3, 1, 1, 3, 3, 3},
    }
    for _, test := range tests {
        nest := toTree(test)
        fmt.Printf("%17s => %v\n", fmt.Sprintf("%v", test), nest)
    }
}
Task: Tree datastructures




Go: package main

import (
    "fmt"
    "io"
    "os"
    "strings"
)

type nNode struct {
    name     string
    children []nNode
}

type iNode struct {
    level int
    name  string
}

func printNest(n nNode, level int, w io.Writer) {
    if level == 0 {
        fmt.Fprintln(w, "\n==Nest form==\n")
    }
    fmt.Fprintf(w, "%s%s\n", strings.Repeat("  ", level), n.name)
    for _, c := range n.children {
        fmt.Fprintf(w, "%s", strings.Repeat("  ", level+1))
        printNest(c, level+1, w)
    }
}

func toNest(iNodes []iNode, start, level int, n *nNode) {
    if level == 0 {
        n.name = iNodes[0].name
    }
    for i := start + 1; i < len(iNodes); i++ {
        if iNodes[i].level == level+1 {
            c := nNode{iNodes[i].name, nil}
            toNest(iNodes, i, level+1, &c)
            n.children = append(n.children, c)
        } else if iNodes[i].level <= level {
            return
        }
    }
}

func printIndent(iNodes []iNode, w io.Writer) {
    fmt.Fprintln(w, "\n==Indent form==\n")
    for _, n := range iNodes {
        fmt.Fprintf(w, "%d %s\n", n.level, n.name)
    }
}

func toIndent(n nNode, level int, iNodes *[]iNode) {
    *iNodes = append(*iNodes, iNode{level, n.name})
    for _, c := range n.children {
        toIndent(c, level+1, iNodes)
    }
}

func main() {
    n1 := nNode{"RosettaCode", nil}
    n2 := nNode{"rocks", []nNode{{"code", nil}, {"comparison", nil}, {"wiki", nil}}}
    n3 := nNode{"mocks", []nNode{{"trolling", nil}}}
    n1.children = append(n1.children, n2, n3)

    var sb strings.Builder
    printNest(n1, 0, &sb)
    s1 := sb.String()
    fmt.Print(s1)

    var iNodes []iNode
    toIndent(n1, 0, &iNodes)
    printIndent(iNodes, os.Stdout)

    var n nNode
    toNest(iNodes, 0, 0, &n)
    sb.Reset()
    printNest(n, 0, &sb)
    s2 := sb.String()
    fmt.Print(s2)

    fmt.Println("\nRound trip test satisfied? ", s1 == s2)
}
Task: Pell numbers




Go: package main

import (
    "fmt"
    "math/big"
    "rcu"
)

func main() {
    p := make([]int64, 40)
    p[1] = 1
    for i := 2; i < 40; i++ {
        p[i] = 2*p[i-1] + p[i-2]
    }
    fmt.Println("The first 20 Pell numbers are:")
    fmt.Println(p[0:20])

    q := make([]int64, 40)
    q[0] = 2
    q[1] = 2
    for i := 2; i < 40; i++ {
        q[i] = 2*q[i-1] + q[i-2]
    }
    fmt.Println("\nThe first 20 Pell-Lucas numbers are:")
    fmt.Println(q[0:20])

    fmt.Println("\nThe first 20 rational approximations of √2 (1.4142135623730951) are:")
    for i := 1; i <= 20; i++ {
        r := big.NewRat(q[i]/2, p[i])
        fmt.Printf("%-17s ≈ %-18s\n", r, r.FloatString(16))
    }

    fmt.Println("\nThe first 15 Pell primes are:")
    p0 := big.NewInt(0)
    p1 := big.NewInt(1)
    p2 := big.NewInt(0)
    two := big.NewInt(2)
    indices := make([]int, 15)
    for index, count := 2, 0; count < 15; index++ {
        p2.Mul(p1, two)
        p2.Add(p2, p0)
        if rcu.IsPrime(index) && p2.ProbablyPrime(15) {
            fmt.Println(p2)
            indices[count] = index
            count++
        }
        p0.Set(p1)
        p1.Set(p2)
    }

    fmt.Println("\nIndices of the first 15 Pell primes are:")
    fmt.Println(indices)

    fmt.Println("\nFirst 20 Newman-Shank_Williams numbers:")
    nsw := make([]int64, 20)
    for n := 0; n < 20; n++ {
        nsw[n] = p[2*n] + p[2*n+1]
    }
    fmt.Println(nsw)

    fmt.Println("\nFirst 20 near isosceles right triangles:")
    u0 := 0
    u1 := 1
    sum := 1
    for i := 2; i < 43; i++ {
        u2 := u1*2 + u0
        if i%2 == 1 {
            fmt.Printf("(%d, %d, %d)\n", sum, sum+1, u2)
        }
        sum += u2
        u0 = u1
        u1 = u2
    }
}
Task: Simulated annealing




Go: package main

import (
    "fmt"
    "math"
    "math/rand"
    "time"
)

var (
    dists = calcDists()
    dirs  = [8]int{1, -1, 10, -10, 9, 11, -11, -9} 
)


func calcDists() []float64 {
    dists := make([]float64, 10000)
    for i := 0; i < 10000; i++ {
        ab, cd := math.Floor(float64(i)/100), float64(i%100)
        a, b := math.Floor(ab/10), float64(int(ab)%10)
        c, d := math.Floor(cd/10), float64(int(cd)%10)
        dists[i] = math.Hypot(a-c, b-d)
    }
    return dists
}


func dist(ci, cj int) float64 {
    return dists[cj*100+ci]
}


func Es(path []int) float64 {
    d := 0.0
    for i := 0; i < len(path)-1; i++ {
        d += dist(path[i], path[i+1])
    }
    return d
}


func T(k, kmax, kT int) float64 {
    return (1 - float64(k)/float64(kmax)) * float64(kT)
}


func dE(s []int, u, v int) float64 {
    su, sv := s[u], s[v]
    
    a, b, c, d := dist(s[u-1], su), dist(s[u+1], su), dist(s[v-1], sv), dist(s[v+1], sv)
    
    na, nb, nc, nd := dist(s[u-1], sv), dist(s[u+1], sv), dist(s[v-1], su), dist(s[v+1], su)
    if v == u+1 {
        return (na + nd) - (a + d)
    } else if u == v+1 {
        return (nc + nb) - (c + b)
    } else {
        return (na + nb + nc + nd) - (a + b + c + d)
    }
}


func P(deltaE float64, k, kmax, kT int) float64 {
    return math.Exp(-deltaE / T(k, kmax, kT))
}

func sa(kmax, kT int) {
    rand.Seed(time.Now().UnixNano())
    temp := make([]int, 99)
    for i := 0; i < 99; i++ {
        temp[i] = i + 1
    }
    rand.Shuffle(len(temp), func(i, j int) {
        temp[i], temp[j] = temp[j], temp[i]
    })
    s := make([]int, 101) 
    copy(s[1:], temp)     
    fmt.Println("kT =", kT)
    fmt.Printf("E(s0) %f\n\n", Es(s)) 
    Emin := Es(s)                     
    for k := 0; k <= kmax; k++ {
        if k%(kmax/10) == 0 {
            fmt.Printf("k:%10d   T: %8.4f   Es: %8.4f\n", k, T(k, kmax, kT), Es(s))
        }
        u := 1 + rand.Intn(99)          
        cv := s[u] + dirs[rand.Intn(8)] 
        if cv <= 0 || cv >= 100 {       
            continue
        }
        if dist(s[u], cv) > 5 { 
            continue
        }
        v := s[cv] 
        deltae := dE(s, u, v)
        if deltae < 0 || 
            P(deltae, k, kmax, kT) >= rand.Float64() {
            s[u], s[v] = s[v], s[u]
            Emin += deltae
        }
    }
    fmt.Printf("\nE(s_final) %f\n", Emin)
    fmt.Println("Path:")
    
    for i := 0; i < len(s); i++ {
        if i > 0 && i%10 == 0 {
            fmt.Println()
        }
        fmt.Printf("%4d", s[i])
    }
    fmt.Println()
}

func main() {
    sa(1e6, 1)
}