Task: Sexy primes




Go: package main

import "fmt"

func sieve(limit int) []bool {
    limit++
    
    c := make([]bool, limit) 
    c[0] = true
    c[1] = true
    
    p := 3 
    for {
        p2 := p * p
        if p2 >= limit {
            break
        }
        for i := p2; i < limit; i += 2 * p {
            c[i] = true
        }
        for {
            p += 2
            if !c[p] {
                break
            }
        }
    }
    return c
}

func commatize(n int) string {
    s := fmt.Sprintf("%d", n)
    if n < 0 {
        s = s[1:]
    }
    le := len(s)
    for i := le - 3; i >= 1; i -= 3 {
        s = s[0:i] + "," + s[i:]
    }
    if n >= 0 {
        return s
    }
    return "-" + s
}

func printHelper(cat string, le, lim, max int) (int, int, string) {
    cle, clim := commatize(le), commatize(lim)
    if cat != "unsexy primes" {
        cat = "sexy prime " + cat
    }
    fmt.Printf("Number of %s less than %s = %s\n", cat, clim, cle)
    last := max
    if le < last {
        last = le
    }
    verb := "are"
    if last == 1 {
        verb = "is"
    }
    return le, last, verb
}

func main() {
    lim := 1000035
    sv := sieve(lim - 1)
    var pairs [][2]int
    var trips [][3]int
    var quads [][4]int
    var quins [][5]int
    var unsexy = []int{2, 3}
    for i := 3; i < lim; i += 2 {
        if i > 5 && i < lim-6 && !sv[i] && sv[i-6] && sv[i+6] {
            unsexy = append(unsexy, i)
            continue
        }
        if i < lim-6 && !sv[i] && !sv[i+6] {
            pair := [2]int{i, i + 6}
            pairs = append(pairs, pair)
        } else {
            continue
        }
        if i < lim-12 && !sv[i+12] {
            trip := [3]int{i, i + 6, i + 12}
            trips = append(trips, trip)
        } else {
            continue
        }
        if i < lim-18 && !sv[i+18] {
            quad := [4]int{i, i + 6, i + 12, i + 18}
            quads = append(quads, quad)
        } else {
            continue
        }
        if i < lim-24 && !sv[i+24] {
            quin := [5]int{i, i + 6, i + 12, i + 18, i + 24}
            quins = append(quins, quin)
        }
    }
    le, n, verb := printHelper("pairs", len(pairs), lim, 5)
    fmt.Printf("The last %d %s:\n  %v\n\n", n, verb, pairs[le-n:])

    le, n, verb = printHelper("triplets", len(trips), lim, 5)
    fmt.Printf("The last %d %s:\n  %v\n\n", n, verb, trips[le-n:])

    le, n, verb = printHelper("quadruplets", len(quads), lim, 5)
    fmt.Printf("The last %d %s:\n  %v\n\n", n, verb, quads[le-n:])

    le, n, verb = printHelper("quintuplets", len(quins), lim, 5)
    fmt.Printf("The last %d %s:\n  %v\n\n", n, verb, quins[le-n:])

    le, n, verb = printHelper("unsexy primes", len(unsexy), lim, 10)
    fmt.Printf("The last %d %s:\n  %v\n\n", n, verb, unsexy[le-n:])
}




Python: LIMIT = 1_000_035
def primes2(limit=LIMIT):
    if limit < 2: return []
    if limit < 3: return [2]
    lmtbf = (limit - 3) // 2
    buf = [True] * (lmtbf + 1)
    for i in range((int(limit ** 0.5) - 3) // 2 + 1):
        if buf[i]:
            p = i + i + 3
            s = p * (i + 1) + i
            buf[s::p] = [False] * ((lmtbf - s) // p + 1)
    return [2] + [i + i + 3 for i, v in enumerate(buf) if v]

primes = primes2(LIMIT +6)
primeset = set(primes)
primearray = [n in primeset for n in range(LIMIT)]


s = [[] for x in range(4)]
unsexy = []

for p in primes:
    if p > LIMIT:
        break
    if p + 6 in primeset and p + 6 < LIMIT:
        s[0].append((p, p+6))
    elif p + 6 in primeset:
        break
    else:
        if p - 6 not in primeset:
            unsexy.append(p)
        continue
    if p + 12 in primeset and p + 12 < LIMIT:
        s[1].append((p, p+6, p+12))
    else:
        continue
    if p + 18 in primeset and p + 18 < LIMIT:
        s[2].append((p, p+6, p+12, p+18))
    else:
        continue
    if p + 24 in primeset and p + 24 < LIMIT:
        s[3].append((p, p+6, p+12, p+18, p+24))


print('"SEXY" PRIME GROUPINGS:')
for sexy, name in zip(s, 'pairs triplets quadruplets quintuplets'.split()):
    print(f'  {len(sexy)} {na (not isPrime(n-6))))) |> Array.ofSeq
printfn "There are %d unsexy primes less than 1,000,035. The last 10 are:" n.Length
Array.skip (n.Length-10) n |> Array.iter(fun n->printf "%d " n); printfn ""
let ni=pCache |> Seq.takeWhile(fun n->nme} ending with ...')
    for sx in sexy[-5:]:
        print('   ',sx)

print(f'\nThere are {len(unsexy)} unsexy primes ending with ...')
for usx in unsexy[-10:]:
    print(' ',usx)