Transaction
377E660AEC63E7…EDDFF500D72A
Block 26,001 · index 0 · indexed
Summary
- Hash
- 377E660AEC63E73D62EC274C592C34A952F5DE9D8D44B63330C8EDDFF500D72A
- Block
- 26,001
- Size
- 6970 bytes
- Gas used
- 11,623,889 / 13,948,726
- Fee
- 1000000ugnot
- Memo
- gnopublish
- Status
- success
Messages
Arguments · 9
- #1collatz
- #2README.md
- #3# Collatz Explorer > ⚠️ **Experimental — generated with no human supervision.** This realm was > produced automatically by an MCP-driven agent to exercise the gno MCP server > and tooling, and to generate test content for gno compilers, linters and > formatters. **Not audited. Not for production.** Full context & folder README: > [r/moul/x/daily](https://github.com/moul/gno-contracts/blob/main/r/moul/x/daily/README.md) --- A hailstone-sequence explorer realm. Call `Compute(n)` to walk the Collatz sequence for a positive integer `n` — the realm records its stopping time (number of steps to reach 1) and peak value, attributed to your address. The root page shows a leaderboard of the longest sequences submitted, and `/<n>` renders the full hailstone sequence for any `n` on the fly. Realm path: `gno.land/r/REPLACE_ADDR/collatz` ## Example calls ``` # record a run for n = 27 (111 steps, peak 9232) gnokey maketx call -pkgpath "gno.land/r/REPLACE_ADDR/collatz" \ -func Compute -args 27 ... # view the leaderboard gnokey query vm/qrender --data "gno.land/r/REPLACE_ADDR/collatz:" # view the full hailstone sequence for 27 gnokey query vm/qrender --data "gno.land/r/REPLACE_ADDR/collatz:/27" ``` <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/moul/gno-contracts)** — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage. **Dependency graph:**  > 🧪 **Highly experimental — potentially vibe-coded.** Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: [DISCLAIMER](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
- #4collatz.gno
- #5package collatz import ( "chain" "chain/runtime/unsafe" "sort" "strconv" "strings" "gno.land/p/nt/avl/v0" ) // maxIters caps the Collatz walk so a hostile / huge input cannot loop // forever on-chain. No known n stays below this bound and fails to reach 1. const maxIters = 1000000 // maxSeqLen caps how many terms Render("/<n>") will print. const maxSeqLen = 500 // Result is one recorded submission. type Result struct { N int64 Steps int Peak int64 Who address } // results maps a decimal-string key of N -> Result. avl.Tree keeps a // deterministic order for iteration inside Render. var results avl.Tree // stopping returns the number of steps to reach 1 and the peak value seen, // walking the Collatz (hailstone) map. It never mutates state. func stopping(n int64) (steps int, peak int64) { peak = n cur := n for cur != 1 { if steps >= maxIters { break } if cur%2 == 0 { cur = cur / 2 } else { cur = 3*cur + 1 } if cur > peak { peak = cur } steps++ } return steps, peak } // Compute walks the hailstone sequence for n and records the result // attributed to the caller. It is a crossing (state-mutating) func. func Compute(cur realm, n int) { if n <= 0 { panic("n must be > 0") } caller := unsafe.PreviousRealm().Address() nn := int64(n) steps, peak := stopping(nn) res := Result{N: nn, Steps: steps, Peak: peak, Who: caller} key := strconv.FormatInt(nn, 10) results.Set(key, res) chain.Emit( "Computed", "n", key, "steps", strconv.Itoa(steps), "peak", strconv.FormatInt(peak, 10), "who", caller.String(), ) } // byLongest implements sort.Interface, ordering results by steps desc, // then peak desc, then n asc for stability. type byLongest []Result func (b byLongest) Len() int { return len(b) } func (b byLongest) Swap(i, j int) { b[i], b[j] = b[j], b[i] } func (b byLongest) Less(i, j int) bool { if b[i].Steps != b[j].Steps { return b[i].Steps > b[j].Steps } if b[i].Peak != b[j].Peak { return b[i].Peak > b[j].Peak } return b[i].N < b[j].N } // Render shows the leaderboard at root, or the full hailstone sequence for a // given n at path "/<n>". func Render(path string) string { p := strings.TrimPrefix(path, "/") if p == "" { return renderLeaderboard() } return renderSequence(p) } func renderLeaderboard() string { all := make([]Result, 0, results.Size()) results.Iterate("", "", func(_ string, v interface{}) bool { all = append(all, v.(Result)) return false }) var sb strings.Builder sb.WriteString("# Collatz Explorer\n\n") sb.WriteString("Hailstone-sequence explorer. Call `Compute(n)` to record a run, ") sb.WriteString("or view a full sequence at `/<n>`.\n\n") if len(all) == 0 { sb.WriteString("_No sequences submitted yet._\n") return sb.String() } sort.Stable(byLongest(all)) sb.WriteString("## Leaderboard — longest sequences\n\n") sb.WriteString("| # | n | steps | peak | who |\n") sb.WriteString("|---|---|-------|------|-----|\n") limit := len(all) if limit > 20 { limit = 20 } for i := 0; i < limit; i++ { r := all[i] sb.WriteString("| " + strconv.Itoa(i+1) + " | ") sb.WriteString("[" + strconv.FormatInt(r.N, 10) + "](/r:" + strconv.FormatInt(r.N, 10) + ") | ") sb.WriteString(strconv.Itoa(r.Steps) + " | ") sb.WriteString(strconv.FormatInt(r.Peak, 10) + " | ") sb.WriteString(r.Who.String() + " |\n") } sb.WriteString("\n_" + strconv.Itoa(len(all)) + " sequence(s) recorded._\n") return sb.String() } func renderSequence(p string) string { n, err := strconv.ParseInt(p, 10, 64) var sb strings.Builder if err != nil || n <= 0 { sb.WriteString("# Invalid n\n\n`" + p + "` is not a positive integer.\n") return sb.String() } sb.WriteString("# Hailstone sequence for " + strconv.FormatInt(n, 10) + "\n\n") seq := make([]int64, 0, 64) cur := n var peak int64 = n truncated := false for { seq = append(seq, cur) if cur == 1 { break } if len(seq) >= maxSeqLen { truncated = true break } if cur%2 == 0 { cur = cur / 2 } else { cur = 3*cur + 1 } if cur > peak { peak = cur } } parts := make([]string, len(seq)) for i, v := range seq { parts[i] = strconv.FormatInt(v, 10) } sb.WriteString(strings.Join(parts, " → ")) sb.WriteString("\n\n") sb.WriteString("- steps: " + strconv.Itoa(len(seq)-1) + "\n") sb.WriteString("- peak: " + strconv.FormatInt(peak, 10) + "\n") if truncated { sb.WriteString("\n_Sequence truncated at " + strconv.Itoa(maxSeqLen) + " terms._\n") } return sb.String() }
- #6gnomod.toml
- #7module = "gno.land/r/moul/x/daily/collatz/v0" gno = "0.9"
- #8/gno.MemPackageType
- #9 MPUserProd
Result log
msg:0,success:true,log:,events:[]