Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/p/nym-moultest123/x/daily/sieve/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186405
  max_deposit = "5000000ugnot"

sieve.gno

// Package sieve is an on-chain port of Go's classic concurrent prime sieve
// (the "prime sieve" example from the Go tour / Go source docs), implemented
// as a deterministic, allocation-friendly Sieve of Eratosthenes so it runs
// reproducibly on-chain (no goroutines, channels, or clocks) — as a reusable
// pure package.
//
// A live demo of this package (a gnoweb prime explorer) is at
// [r/moul/x/daily/sievedemo](/r/nym-moultest123/x/daily/sievedemo/v0).
package sieve

// MaxN bounds the sieve so gas stays predictable.
const MaxN = 10000

// PrimesUpTo returns every prime p with 2 <= p <= n, in ascending order,
// using the Sieve of Eratosthenes. n is clamped to [0, MaxN]. Pure.
func PrimesUpTo(n int) []int {
	if n < 2 {
		return []int{}
	}
	if n > MaxN {
		n = MaxN
	}

	// composite[i] == true once i is known to be non-prime.
	composite := make([]bool, n+1)
	for p := 2; p*p <= n; p++ {
		if composite[p] {
			continue
		}
		for m := p * p; m <= n; m += p {
			composite[m] = true
		}
	}

	primes := []int{}
	for i := 2; i <= n; i++ {
		if !composite[i] {
			primes = append(primes, i)
		}
	}
	return primes
}

// NthPrime returns the k-th prime (1-indexed), or 0 if it lies beyond MaxN.
// Pure helper handy for callers and tests.
func NthPrime(k int) int {
	if k < 1 {
		return 0
	}
	primes := PrimesUpTo(MaxN)
	if k > len(primes) {
		return 0
	}
	return primes[k-1]
}

// IsPrime reports whether x is prime (trial division). Pure.
func IsPrime(x int) bool {
	if x < 2 {
		return false
	}
	for d := 2; d*d <= x; d++ {
		if x%d == 0 {
			return false
		}
	}
	return true
}

sieve_test.gno

package sieve

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
)

func TestPrimesUpTo(t *testing.T) {
	cases := []struct {
		name string
		n    int
		want []int
	}{
		{"below two", 1, []int{}},
		{"exactly two", 2, []int{2}},
		{"ten", 10, []int{2, 3, 5, 7}},
		{"thirty", 30, []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}},
	}

	for _, tc := range cases {
		got := PrimesUpTo(tc.n)
		uassert.Equal(t, len(tc.want), len(got), tc.name+": length")
		for i := range tc.want {
			if i >= len(got) {
				break
			}
			uassert.Equal(t, tc.want[i], got[i], tc.name+": element")
		}
	}

	// π(100) == 25, π(1000) == 168 — classic sieve landmarks.
	uassert.Equal(t, 25, len(PrimesUpTo(100)), "pi(100)")
	uassert.Equal(t, 168, len(PrimesUpTo(1000)), "pi(1000)")

	// clamping: anything above MaxN yields the same set as MaxN.
	uassert.Equal(t, len(PrimesUpTo(MaxN)), len(PrimesUpTo(MaxN+5000)), "clamp")
}

func TestIsPrimeAndNthPrime(t *testing.T) {
	primeCases := []int{2, 3, 5, 7, 11, 13, 9973}
	for _, p := range primeCases {
		uassert.True(t, IsPrime(p), "expected prime")
	}

	compositeCases := []int{0, 1, 4, 9, 100, 9975}
	for _, c := range compositeCases {
		uassert.False(t, IsPrime(c), "expected non-prime")
	}

	uassert.Equal(t, 2, NthPrime(1), "1st prime")
	uassert.Equal(t, 29, NthPrime(10), "10th prime")
	uassert.Equal(t, 541, NthPrime(100), "100th prime")
	uassert.Equal(t, 0, NthPrime(0), "0th prime is invalid")
}