Playground
factor.gno
// Package factor is a Gauntlet judge: factor a number into primes.
//
// Answers look like "2*2*3*7" (ascending, separated by "*"). Checking an
// answer is cheap: multiply the factors back and test each one for
// primality. Finding them is the work.
package factor
import (
"strconv"
"strings"
"gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet"
)
const max = 10_000_000
func init(cur realm) {
gauntlet.RegisterJudge(cross(cur), "Prime factors",
"Factor n (2 <= n < 10,000,000) as ascending primes joined by *, e.g. 84 -> 2*2*3*7. 6 random n.", Inputs, Check)
}
// Inputs returns 6 random numbers for a seed.
func Inputs(seed uint64) []string {
r := rng{seed}
out := make([]string, 0, 6)
for i := 0; i < 6; i++ {
n := 2 + r.next()%(max-2)
out = append(out, strconv.FormatUint(n, 10))
}
return out
}
// Check reports whether out is the ascending prime factorization of in.
func Check(in, out string) bool {
n, err := strconv.ParseUint(in, 10, 64)
if err != nil || n < 2 {
return false
}
prod := uint64(1)
prev := uint64(0)
for _, f := range strings.Split(out, "*") {
p, err := strconv.ParseUint(strings.TrimSpace(f), 10, 64)
if err != nil || p < prev || !isPrime(p) {
return false
}
prev = p
prod *= p
if prod > n {
return false
}
}
return prod == n
}
func isPrime(p uint64) bool {
if p < 2 {
return false
}
for d := uint64(2); d*d <= p; d++ {
if p%d == 0 {
return false
}
}
return true
}
type rng struct{ s uint64 }
func (r *rng) next() uint64 {
r.s += 0x9e3779b97f4a7c15
z := r.s
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9
z = (z ^ (z >> 27)) * 0x94d049bb133111eb
return z ^ (z >> 31)
}
func Render(_ string) string {
return "# Prime factors judge\n\nA [Gauntlet](/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet:judges) judge. Read the source to see exactly how answers are checked.\n"
}
factor_test.gno
package factor
import "testing"
func TestInputsInRange(t *testing.T) {
ins := Inputs(1)
if len(ins) != 6 {
t.Fatalf("want 6 inputs, got %d", len(ins))
}
for _, in := range ins {
if !Check(in, factorize(in)) {
t.Fatalf("own factorization of %q rejected", in)
}
}
if Inputs(2)[0] == ins[0] {
t.Fatal("different seeds must give different inputs")
}
}
func TestCheck(t *testing.T) {
cases := []struct {
in, out string
want bool
}{
{"84", "2*2*3*7", true},
{"13", "13", true},
{"84", "7*3*2*2", false}, // not ascending
{"84", "2*2*21", false}, // 21 is not prime
{"84", "2*2*3", false}, // product too small
{"84", "2*2*3*7*1", false}, // 1 is not prime
{"84", "84", false}, // 84 is not prime
{"84", "", false},
{"1", "1", false}, // out of range
}
for _, c := range cases {
if got := Check(c.in, c.out); got != c.want {
t.Fatalf("Check(%q, %q) = %v, want %v", c.in, c.out, got, c.want)
}
}
}
// factorize is only used by the test, to prove generated inputs are solvable.
func factorize(in string) string {
n := uint64(0)
for i := 0; i < len(in); i++ {
n = n*10 + uint64(in[i]-'0')
}
out := ""
for d := uint64(2); d*d <= n; d++ {
for n%d == 0 {
if out != "" {
out += "*"
}
out += utoa(d)
n /= d
}
}
if n > 1 {
if out != "" {
out += "*"
}
out += utoa(n)
}
return out
}
func utoa(n uint64) string {
if n == 0 {
return "0"
}
s := ""
for n > 0 {
s = string(rune('0'+n%10)) + s
n /= 10
}
return s
}
gnomod.toml
module = "gno.land/r/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/judges/factor" gno = "0.9" [addpkg] creator = "g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x" height = 1373018 max_deposit = "100000000ugnot"