WeixiangYan/CodeTransOcean huggingface dataset:
Task: Smarandache-Wellin primes




Go: package main

import (
    "fmt"
    big "github.com/ncw/gmp"
    "rcu"
    "strconv"
    "strings"
)

func ord(count int) string {
    if count == 1 {
        return "st"
    }
    if count == 2 {
        return "nd"
    }
    if count == 3 {
        return "rd"
    }
    return "th"
}

func main() {
    primes := rcu.Primes(12000)
    sw := ""
    count := 0
    i := 0
    n := new(big.Int)
    fmt.Println("The known Smarandache-Wellin primes are:")
    for count < 8 {
        sw += strconv.Itoa(primes[i])
        n.SetString(sw, 10)
        if n.ProbablyPrime(15) {
            count++
            sws := sw
            le := len(sws)
            if le > 4 {
                sws = sws[0:20] + "..." + sws[le-20:le]
            }
            fmt.Printf("%d%s: index %4d  digits %4d  last prime %5d -> %s\n", count, ord(count), i+1, len(sw), primes[i], sws)
        }
        i++
    }

    fmt.Println("\nThe first 20 Derived Smarandache-Wellin primes are:")
    freqs := make([]int, 10)
    count = 0
    i = 0
    for count < 20 {
        p := strconv.Itoa(primes[i])
        for _, d := range p {
            n, _ := strconv.Atoi(string(d))
            freqs[n]++
        }
        dsw := ""
        for _, freq := range freqs {
            dsw += strconv.Itoa(freq)
        }
        dsw = strings.TrimLeft(dsw, "0")
        n.SetString(dsw, 10)
        if n.ProbablyPrime(15) {
            count++
            fmt.Printf("%2d%s: index %4d  prime %v\n", count, ord(count), i+1, n)
        }
        i++
    }
}
Task: Verhoeff algorithm




Go: package main

import "fmt"

var d = [][]int{
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    {1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
    {2, 3, 4, 0, 1, 7, 8, 9, 5, 6},
    {3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
    {4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
    {5, 9, 8, 7, 6, 0, 4, 3, 2, 1},
    {6, 5, 9, 8, 7, 1, 0, 4, 3, 2},
    {7, 6, 5, 9, 8, 2, 1, 0, 4, 3},
    {8, 7, 6, 5, 9, 3, 2, 1, 0, 4},
    {9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
}

var inv = []int{0, 4, 3, 2, 1, 5, 6, 7, 8, 9}

var p = [][]int{
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    {1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
    {5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
    {8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
    {9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
    {4, 2, 8, 6, 5, 7, 3, 9, 0, 1},
    {2, 7, 9, 3, 8, 0, 6, 4, 1, 5},
    {7, 0, 4, 6, 9, 1, 3, 2, 5, 8},
}

func verhoeff(s string, validate, table bool) interface{} {
    if table {
        t := "Check digit"
        if validate {
            t = "Validation"
        }
        fmt.Printf("%s calculations for '%s':\n\n", t, s)
        fmt.Println(" i  nᵢ  p[i,nᵢ]  c")
        fmt.Println("------------------")
    }
    if !validate {
        s = s + "0"
    }
    c := 0
    le := len(s) - 1
    for i := le; i >= 0; i-- {
        ni := int(s[i] - 48)
        pi := p[(le-i)%8][ni]
        c = d[c][pi]
        if table {
            fmt.Printf("%2d  %d      %d     %d\n", le-i, ni, pi, c)
        }
    }
    if table && !validate {
        fmt.Printf("\ninv[%d] = %d\n", c, inv[c])
    }
    if !validate {
        return inv[c]
    }
    return c == 0
}

func main() {
    ss := []string{"236", "12345", "123456789012"}
    ts := []bool{true, true, false, true}
    for i, s := range ss {
        c := verhoeff(s, false, ts[i]).(int)
        fmt.Printf("\nThe check digit for '%s' is '%d'\n\n", s, c)
        for _, sc := range []string{s + string(c+48), s + "9"} {
            v := verhoeff(sc, true, ts[i]).(bool)
            ans := "correct"
            if !v {
                ans = "incorrect"
            }
            fmt.Printf("\nThe validation for '%s' is %s\n\n", sc, ans)
        }
    }
}
Task: Padovan n-step number sequences




Go: package main

import "fmt"

func padovanN(n, t int) []int {
    if n < 2 || t < 3 {
        ones := make([]int, t)
        for i := 0; i < t; i++ {
            ones[i] = 1
        }
        return ones
    }
    p := padovanN(n-1, t)
    for i := n + 1; i < t; i++ {
        p[i] = 0
        for j := i - 2; j >= i-n-1; j-- {
            p[i] += p[j]
        }
    }
    return p
}

func main() {
    t := 15
    fmt.Println("First", t, "terms of the Padovan n-step number sequences:")
    for n := 2; n <= 8; n++ {
        fmt.Printf("%d: %3d\n", n, padovanN(n, t))
    }
}
Task: Prime triangle




Go: package main

import "fmt"

var canFollow [][]bool
var arrang []int
var bFirst = true

var pmap = make(map[int]bool)

func init() {
    for _, i := range []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37} {
        pmap[i] = true
    }
}

func ptrs(res, n, done int) int {
    ad := arrang[done-1]
    if n-done <= 1 {
        if canFollow[ad-1][n-1] {
            if bFirst {
                for _, e := range arrang {
                    fmt.Printf("%2d ", e)
                }
                fmt.Println()
                bFirst = false
            }
            res++
        }
    } else {
        done++
        for i := done - 1; i <= n-2; i += 2 {
            ai := arrang[i]
            if canFollow[ad-1][ai-1] {
                arrang[i], arrang[done-1] = arrang[done-1], arrang[i]
                res = ptrs(res, n, done)
                arrang[i], arrang[done-1] = arrang[done-1], arrang[i]
            }
        }
    }
    return res
}

func primeTriangle(n int) int {
    canFollow = make([][]bool, n)
    for i := 0; i < n; i++ {
        canFollow[i] = make([]bool, n)
        for j := 0; j < n; j++ {
            _, ok := pmap[i+j+2]
            canFollow[i][j] = ok
        }
    }
    bFirst = true
    arrang = make([]int, n)
    for i := 0; i < n; i++ {
        arrang[i] = i + 1
    }
    return ptrs(0, n, 1)
}

func main() {
    counts := make([]int, 19)
    for i := 2; i <= 20; i++ {
        counts[i-2] = primeTriangle(i)
    }
    fmt.Println()
    for i := 0; i < 19; i++ {
        fmt.Printf("%d ", counts[i])
    }
    fmt.Println()
}
Task: Sphenic numbers




Go: package main

import (
    "fmt"
    "math"
    "rcu"
    "sort"
)

func main() {
    const limit = 1000000
    limit2 := int(math.Cbrt(limit)) 
    primes := rcu.Primes(limit / 6)
    pc := len(primes)
    var sphenic []int
    fmt.Println("Sphenic numbers less than 1,000:")
    for i := 0; i < pc-2; i++ {
        if primes[i] > limit2 {
            break
        }
        for j := i + 1; j < pc-1; j++ {
            prod := primes[i] * primes[j]
            if prod+primes[j+1] >= limit {
                break
            }
            for k := j + 1; k < pc; k++ {
                res := prod * primes[k]
                if res >= limit {
                    break
                }
                sphenic = append(sphenic, res)
            }
        }
    }
    sort.Ints(sphenic)
    ix := sort.Search(len(sphenic), func(i int) bool { return sphenic[i] >= 1000 })
    rcu.PrintTable(sphenic[:ix], 15, 3, false)
    fmt.Println("\nSphenic triplets less than 10,000:")
    var triplets [][3]int
    for i := 0; i < len(sphenic)-2; i++ {
        s := sphenic[i]
        if sphenic[i+1] == s+1 && sphenic[i+2] == s+2 {
            triplets = append(triplets, [3]int{s, s + 1, s + 2})
        }
    }
    ix = sort.Search(len(triplets), func(i int) bool { return triplets[i][2] >= 10000 })
    for i := 0; i < ix; i++ {
        fmt.Printf("%4d ", triplets[i])
        if (i+1)%3 == 0 {
            fmt.Println()
        }
    }
    fmt.Printf("\nThere are %s sphenic numbers less than 1,000,000.\n", rcu.Commatize(len(sphenic)))
    fmt.Printf("There are %s sphenic triplets less than 1,000,000.\n", rcu.Commatize(len(triplets)))
    s := sphenic[199999]
    pf := rcu.PrimeFactors(s)
    fmt.Printf("The 200,000th sphenic number is %s (%d*%d*%d).\n", rcu.Commatize(s), pf[0], pf[1], pf[2])
    fmt.Printf("The 5,000th sphenic triplet is %v.\n.", triplets[4999])
}
Task: Honaker primes




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    primes := rcu.Primes(5 * 1e6)
    var h [][2]int
    var h10000 [2]int
    for i, count := 1, 0; count < 10000; i++ {
        if rcu.DigitSum(i, 10) == rcu.DigitSum(primes[i-1], 10) {
            count++
            if count <= 50 {
                h = append(h, [2]int{i, primes[i-1]})
            } else if count == 10000 {
                h10000 = [2]int{i, primes[i-1]}
            }
        }
    }
    fmt.Println("The first 50 Honaker primes (index, prime):\n")
    for i := 0; i < 50; i++ {
        fmt.Printf("(%3d, %5s) ", h[i][0], rcu.Commatize(h[i][1]))
        if (i+1)%5 == 0 {
            fmt.Println()
        }
    }
    fmt.Printf("\nand the 10,000th: (%7s, %9s)\n", rcu.Commatize(h10000[0]), rcu.Commatize(h10000[1]))
}
Task: XML validation




Go: package main

import (
    "fmt"
    "github.com/lestrrat-go/libxml2"
    "github.com/lestrrat-go/libxml2/xsd"
    "io/ioutil"
    "log"
    "os"
)

func check(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {
    xsdfile := "shiporder.xsd"
    f, err := os.Open(xsdfile)
    check(err)
    defer f.Close()

    buf, err := ioutil.ReadAll(f)
    check(err)

    s, err := xsd.Parse(buf)
    check(err)
    defer s.Free()

    xmlfile := "shiporder.xml"
    f2, err := os.Open(xmlfile)
    check(err)
    defer f2.Close()

    buf2, err := ioutil.ReadAll(f2)
    check(err)

    d, err := libxml2.Parse(buf2)
    check(err)

    if err := s.Validate(d); err != nil {
        for _, e := range err.(xsd.SchemaValidationError).Errors() {
            log.Printf("error: %s", e.Error())
        }
        return
    }

    fmt.Println("Validation of", xmlfile, "against", xsdfile, "successful!")
}
Task: Own digits power sum




Go: package main

import (
    "fmt"
    "math"
    "rcu"
)

func main() {
    powers := [10]int{0, 1, 4, 9, 16, 25, 36, 49, 64, 81}
    fmt.Println("Own digits power sums for N = 3 to 9 inclusive:")
    for n := 3; n < 10; n++ {
        for d := 2; d < 10; d++ {
            powers[d] *= d
        }
        i := int(math.Pow(10, float64(n-1)))
        max := i * 10
        lastDigit := 0
        sum := 0
        var digits []int
        for i < max {
            if lastDigit == 0 {
                digits = rcu.Digits(i, 10)
                sum = 0
                for _, d := range digits {
                    sum += powers[d]
                }
            } else if lastDigit == 1 {
                sum++
            } else {
                sum += powers[lastDigit] - powers[lastDigit-1]
            }
            if sum == i {
                fmt.Println(i)
                if lastDigit == 0 {
                    fmt.Println(i + 1)
                }
                i += 10 - lastDigit
                lastDigit = 0
            } else if sum > i {
                i += 10 - lastDigit
                lastDigit = 0
            } else if lastDigit < 9 {
                i++
                lastDigit++
            } else {
                i++
                lastDigit = 0
            }
        }
    }
}
Task: Ormiston triples




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    const limit = 1e10
    primes := rcu.Primes(limit)
    var orm25 []int
    j := int(1e9)
    count := 0
    var counts []int
    for i := 0; i < len(primes)-2; i++ {
        p1 := primes[i]
        p2 := primes[i+1]
        p3 := primes[i+2]
        if (p2-p1)%18 != 0 || (p3-p2)%18 != 0 {
            continue
        }
        key1 := 1
        for _, dig := range rcu.Digits(p1, 10) {
            key1 *= primes[dig]
        }
        key2 := 1
        for _, dig := range rcu.Digits(p2, 10) {
            key2 *= primes[dig]
        }
        if key1 != key2 {
            continue
        }
        key3 := 1
        for _, dig := range rcu.Digits(p3, 10) {
            key3 *= primes[dig]
        }
        if key2 == key3 {
            if count < 25 {
                orm25 = append(orm25, p1)
            }
            if p1 >= j {
                counts = append(counts, count)
                j *= 10
            }
            count++
        }
    }
    counts = append(counts, count)
    fmt.Println("Smallest members of first 25 Ormiston triples:")
    for i := 0; i < 25; i++ {
        fmt.Printf("%8v ", orm25[i])
        if (i+1)%5 == 0 {
            fmt.Println()
        }
    }
    fmt.Println()
    j = int(1e9)
    for i := 0; i < len(counts); i++ {
        fmt.Printf("%s Ormiston triples before %s\n", rcu.Commatize(counts[i]), rcu.Commatize(j))
        j *= 10
        fmt.Println()
    }
}
Task: Metallic ratios




Go: package main

import (
    "fmt"
    "math/big"
)

var names = [10]string{"Platinum", "Golden", "Silver", "Bronze", "Copper",
    "Nickel", "Aluminium", "Iron", "Tin", "Lead"}

func lucas(b int64) {
    fmt.Printf("Lucas sequence for %s ratio, where b = %d:\n", names[b], b)
    fmt.Print("First 15 elements: ")
    var x0, x1 int64 = 1, 1
    fmt.Printf("%d, %d", x0, x1)
    for i := 1; i <= 13; i++ {
        x2 := b*x1 + x0
        fmt.Printf(", %d", x2)
        x0, x1 = x1, x2
    }
    fmt.Println()
}

func metallic(b int64, dp int) {
    x0, x1, x2, bb := big.NewInt(1), big.NewInt(1), big.NewInt(0), big.NewInt(b)
    ratio := big.NewRat(1, 1)
    iters := 0
    prev := ratio.FloatString(dp)
    for {
        iters++
        x2.Mul(bb, x1)
        x2.Add(x2, x0)
        this := ratio.SetFrac(x2, x1).FloatString(dp)
        if prev == this {
            plural := "s"
            if iters == 1 {
                plural = " "
            }
            fmt.Printf("Value to %d dp after %2d iteration%s: %s\n\n", dp, iters, plural, this)
            return
        }
        prev = this
        x0.Set(x1)
        x1.Set(x2)
    }
}

func main() {
    for b := int64(0); b < 10; b++ {
        lucas(b)
        metallic(b, 32)
    }
    fmt.Println("Golden ratio, where b = 1:")
    metallic(1, 256)
}
Task: Singly-linked list_Element removal




Go: package main

import "fmt"

type Ele struct {
    Data interface{}
    Next *Ele
}

var head *Ele

func (e *Ele) Append(data interface{}) *Ele {
    if e == nil {
        return e
    }
    if e.Next == nil {
        e.Next = &Ele{data, nil}
    } else {
        e.Next = &Ele{data, e.Next}
    }
    return e.Next
}




func (e *Ele) Remove(data interface{}) {
    if e == nil {
        return
    }
    if e.Data == data {
        if e == head {
            head = e.Next
        }
        e.Next = nil
        return
    }
    prev := e
    for iter := e.Next; iter != nil; iter = iter.Next {
        if iter.Data == data {
            prev.Next = iter.Next
            iter.Next = nil
            return
        }
        prev = iter
    }
}

func (e *Ele) String() string {
    return fmt.Sprintf("%v", e.Data)
}

func (e *Ele) Traverse() {
    if e == nil {
        fmt.Println(e)
        return
    }
    for iter := e; iter != nil; iter = iter.Next {
        fmt.Println(iter)
    }
}

func main() {
    head = &Ele{"tacos", nil}
    next := head.Append("burritos")
    next = next.Append("fajitas")
    next = next.Append("enchilatas")

    fmt.Println("Before any removals:")
    head.Traverse()

    head.Remove("fajitas")
    fmt.Println("\nAfter removing fajitas:")
    head.Traverse()

    head.Remove("tacos")
    fmt.Println("\nAfter removing tacos:")
    head.Traverse()

    head.Remove("enchilatas")
    fmt.Println("\nAfter removing enchilatas:")
    head.Traverse()

    head.Remove("burritos")
    fmt.Println("\nAfter removing burritos:")
    head.Traverse()
}
Task: Data Encryption Standard




Go: package main

import (
    "crypto/des"
    "encoding/hex"
    "fmt"
    "log"
)

func main() {
    key, err := hex.DecodeString("0e329232ea6d0d73")
    if err != nil {
        log.Fatal(err)
    }
    c, err := des.NewCipher(key)
    if err != nil {
        log.Fatal(err)
    }
    src, err := hex.DecodeString("8787878787878787")
    if err != nil {
        log.Fatal(err)
    }
    dst := make([]byte, des.BlockSize)
    c.Encrypt(dst, src)
    fmt.Printf("%x\n", dst)
}
Task: Ramanujan primes




Go: package main

import (
    "fmt"
    "math"
    "rcu"
    "time"
)

var count []int

func primeCounter(limit int) {
    count = make([]int, limit)
    for i := 0; i < limit; i++ {
        count[i] = 1
    }
    if limit > 0 {
        count[0] = 0
    }
    if limit > 1 {
        count[1] = 0
    }
    for i := 4; i < limit; i += 2 {
        count[i] = 0
    }
    for p, sq := 3, 9; sq < limit; p += 2 {
        if count[p] != 0 {
            for q := sq; q < limit; q += p << 1 {
                count[q] = 0
            }
        }
        sq += (p + 1) << 2
    }
    sum := 0
    for i := 0; i < limit; i++ {
        sum += count[i]
        count[i] = sum
    }
}

func primeCount(n int) int {
    if n < 1 {
        return 0
    }
    return count[n]
}

func ramanujanMax(n int) int {
    fn := float64(n)
    return int(math.Ceil(4 * fn * math.Log(4*fn)))
}

func ramanujanPrime(n int) int {
    if n == 1 {
        return 2
    }
    for i := ramanujanMax(n); i >= 2*n; i-- {
        if i%2 == 1 {
            continue
        }
        if primeCount(i)-primeCount(i/2) < n {
            return i + 1
        }
    }
    return 0
}

func main() {
    start := time.Now()
    primeCounter(1 + ramanujanMax(1e6))
    fmt.Println("The first 100 Ramanujan primes are:")
    rams := make([]int, 100)
    for n := 0; n < 100; n++ {
        rams[n] = ramanujanPrime(n + 1)
    }
    for i, r := range rams {
        fmt.Printf("%5s ", rcu.Commatize(r))
        if (i+1)%10 == 0 {
            fmt.Println()
        }
    }

    fmt.Printf("\nThe 1,000th Ramanujan prime is %6s\n", rcu.Commatize(ramanujanPrime(1000)))

    fmt.Printf("\nThe 10,000th Ramanujan prime is %7s\n", rcu.Commatize(ramanujanPrime(10000)))

    fmt.Printf("\nThe 100,000th Ramanujan prime is %6s\n", rcu.Commatize(ramanujanPrime(100000)))

    fmt.Printf("\nThe 1,000,000th Ramanujan prime is %7s\n", rcu.Commatize(ramanujanPrime(1000000)))

    fmt.Println("\nTook", time.Since(start))
}
Task: Repunit primes




Go: package main

import (
    "fmt"
    big "github.com/ncw/gmp"
    "rcu"
    "strings"
)

func main() {
    limit := 2700
    primes := rcu.Primes(limit)
    s := new(big.Int)
    for b := 2; b <= 36; b++ {
        var rPrimes []int
        for _, p := range primes {
            s.SetString(strings.Repeat("1", p), b)
            if s.ProbablyPrime(15) {
                rPrimes = append(rPrimes, p)
            }
        }
        fmt.Printf("Base %2d: %v\n", b, rPrimes)
    }
}
Task: Multi-base primes




Go: package main

import (
    "fmt"
    "math"
    "rcu"
)

var maxDepth = 6
var maxBase = 36
var c = rcu.PrimeSieve(int(math.Pow(float64(maxBase), float64(maxDepth))), true)
var digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var maxStrings [][][]int
var mostBases = -1

func maxSlice(a []int) int {
    max := 0
    for _, e := range a {
        if e > max {
            max = e
        }
    }
    return max
}

func maxInt(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func process(indices []int) {
    minBase := maxInt(2, maxSlice(indices)+1)
    if maxBase - minBase + 1 < mostBases {
        return  
    }
    var bases []int
    for b := minBase; b <= maxBase; b++ {
        n := 0
        for _, i := range indices {
            n = n*b + i
        }
        if !c[n] {
            bases = append(bases, b)
        }
    }
    count := len(bases)
    if count > mostBases {
        mostBases = count
        indices2 := make([]int, len(indices))
        copy(indices2, indices)
        maxStrings = [][][]int{[][]int{indices2, bases}}
    } else if count == mostBases {
        indices2 := make([]int, len(indices))
        copy(indices2, indices)
        maxStrings = append(maxStrings, [][]int{indices2, bases})
    }
}

func printResults() {
    fmt.Printf("%d\n", len(maxStrings[0][1]))
    for _, m := range maxStrings {
        s := ""
        for _, i := range m[0] {
            s = s + string(digits[i])
        }
        fmt.Printf("%s -> %v\n", s, m[1])
    }
}

func nestedFor(indices []int, length, level int) {
    if level == len(indices) {
        process(indices)
    } else {
        indices[level] = 0
        if level == 0 {
            indices[level] = 1
        }
        for indices[level] < length {
            nestedFor(indices, length, level+1)
            indices[level]++
        }
    }
}

func main() {
    for depth := 1; depth <= maxDepth; depth++ {
        fmt.Print(depth, " character strings which are prime in most bases: ")
        maxStrings = maxStrings[:0]
        mostBases = -1
        indices := make([]int, depth)
        nestedFor(indices, maxBase, 0)
        printResults()
        fmt.Println()
    }
}
Task: Erdös-Selfridge categorization of primes




Go: package main

import (
    "fmt"
    "math"
    "rcu"
)

var limit = int(math.Log(1e6) * 1e6 * 1.2) 
var primes = rcu.Primes(limit)

var prevCats = make(map[int]int)

func cat(p int) int {
    if v, ok := prevCats[p]; ok {
        return v
    }
    pf := rcu.PrimeFactors(p + 1)
    all := true
    for _, f := range pf {
        if f != 2 && f != 3 {
            all = false
            break
        }
    }
    if all {
        return 1
    }
    if p > 2 {
        len := len(pf)
        for i := len - 1; i >= 1; i-- {
            if pf[i-1] == pf[i] {
                pf = append(pf[:i], pf[i+1:]...)
            }
        }
    }
    for c := 2; c <= 11; c++ {
        all := true
        for _, f := range pf {
            if cat(f) >= c {
                all = false
                break
            }
        }
        if all {
            prevCats[p] = c
            return c
        }
    }
    return 12
}

func main() {
    es := make([][]int, 12)
    fmt.Println("First 200 primes:\n")
    for _, p := range primes[0:200] {
        c := cat(p)
        es[c-1] = append(es[c-1], p)
    }
    for c := 1; c <= 6; c++ {
        if len(es[c-1]) > 0 {
            fmt.Println("Category", c, "\b:")
            fmt.Println(es[c-1])
            fmt.Println()
        }
    }

    fmt.Println("First million primes:\n")
    for _, p := range primes[200:1e6] {
        c := cat(p)
        es[c-1] = append(es[c-1], p)
    }
    for c := 1; c <= 12; c++ {
        e := es[c-1]
        if len(e) > 0 {
            format := "Category %-2d: First = %7d  Last = %8d  Count = %6d\n"
            fmt.Printf(format, c, e[0], e[len(e)-1], len(e))
        }
    }
}
Task: Equal prime and composite sums




Go: package main

import (
    "fmt"
    "log"
    "rcu"
    "sort"
)

func ord(n int) string {
    if n < 0 {
        log.Fatal("Argument must be a non-negative integer.")
    }
    m := n % 100
    if m >= 4 && m <= 20 {
        return fmt.Sprintf("%sth", rcu.Commatize(n))
    }
    m %= 10
    suffix := "th"
    if m == 1 {
        suffix = "st"
    } else if m == 2 {
        suffix = "nd"
    } else if m == 3 {
        suffix = "rd"
    }
    return fmt.Sprintf("%s%s", rcu.Commatize(n), suffix)
}

func main() {
    limit := int(4 * 1e8)
    c := rcu.PrimeSieve(limit-1, true)
    var compSums []int
    var primeSums []int
    csum := 0
    psum := 0
    for i := 2; i < limit; i++ {
        if c[i] {
            csum += i
            compSums = append(compSums, csum)
        } else {
            psum += i
            primeSums = append(primeSums, psum)
        }
    }

    for i := 0; i < len(primeSums); i++ {
        ix := sort.SearchInts(compSums, primeSums[i])
        if ix < len(compSums) && compSums[ix] == primeSums[i] {
            cps := rcu.Commatize(primeSums[i])
            fmt.Printf("%21s - %12s prime sum, %12s composite sum\n", cps, ord(i+1), ord(ix+1))
        }
    }
}