PathrockNetwork Gno Explorer
HomeBlocksTransactionsRealmsPackagesValidatorsAnalytics

PathrockNetwork Gno Explorer — an independent explorer for Gno.land Mainnet (gnoland-1), operated by PathrockNetwork. Not an official Gno.land service.

gnowebarchive RPC

gno.land/p/moul/x/daily/sieve/v0

Package
Open in gnoweb ↗

Overview

Kind
Pure package
Name
v0
Namespace
moul / x / daily / sieve
Files
3 (README)(gnomod.toml)
Exported functions
n/a — not supported for pure packages by the node (vm/qfuncs)
Module
gno.land/p/moul/x/daily/sieve/v0
gno
0.9

Files (3)

  • README.mdmarkdown
  • gnomod.tomltoml
  • sieve.gnogno
sieve.gnogno
1// Package sieve is an on-chain port of Go's classic concurrent prime sieve2// (the "prime sieve" example from the Go tour / Go source docs), implemented3// as a deterministic, allocation-friendly Sieve of Eratosthenes so it runs4// reproducibly on-chain (no goroutines, channels, or clocks) — as a reusable5// pure package.6//7// A live demo of this package (a gnoweb prime explorer) is at8// [r/moul/x/daily/sievedemo](/r/moul/x/daily/sievedemo/v0).9package sieve1011// MaxN bounds the sieve so gas stays predictable.12const MaxN = 100001314// PrimesUpTo returns every prime p with 2 <= p <= n, in ascending order,15// using the Sieve of Eratosthenes. n is clamped to [0, MaxN]. Pure.16func PrimesUpTo(n int) []int {17	if n < 2 {18		return []int{}19	}20	if n > MaxN {21		n = MaxN22	}2324	// composite[i] == true once i is known to be non-prime.25	composite := make([]bool, n+1)26	for p := 2; p*p <= n; p++ {27		if composite[p] {28			continue29		}30		for m := p * p; m <= n; m += p {31			composite[m] = true32		}33	}3435	primes := []int{}36	for i := 2; i <= n; i++ {37		if !composite[i] {38			primes = append(primes, i)39		}40	}41	return primes42}4344// NthPrime returns the k-th prime (1-indexed), or 0 if it lies beyond MaxN.45// Pure helper handy for callers and tests.46func NthPrime(k int) int {47	if k < 1 {48		return 049	}50	primes := PrimesUpTo(MaxN)51	if k > len(primes) {52		return 053	}54	return primes[k-1]55}5657// IsPrime reports whether x is prime (trial division). Pure.58func IsPrime(x int) bool {59	if x < 2 {60		return false61	}62	for d := 2; d*d <= x; d++ {63		if x%d == 0 {64			return false65		}66	}67	return true68}69

Functions

not supported for pure packages by the node (vm/qfuncs)

Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.