SilverSight/julia/hachimoji_encode.jl
allaun 3b6baec64e wip: durability snapshot of local working tree (pre-existing, uncommitted)
Snapshot of previously-uncommitted local work so nothing is lost after the
power outage. NOT reviewed for correctness — a WIP checkpoint, not a feature:
- multi-language hachimoji encoders (c/cpp/fortran/julia/octave/r/scala/go/rust/coq)
- formal Lean WIP (BraidTree, Eisenstein, HachimojiCapture, MathlibConnect,
  ModularFormBridge, ClusterManifold) + lakefile + E8Sidon edit
- docs/, experiments/ (epyc oisc benches), deploy/, scripts, test scaffolding
- .gitignore: exclude **/target/ and Coq build artifacts

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-02 20:49:53 -05:00

149 lines
4.5 KiB
Julia

#!/usr/bin/env julia
using Printf
const LETTER_NAMES = ["\u03A6", "\u039B", "\u03A1", "\u039A", "\u03A9", "\u03A3", "\u03A0", "\u0396"]
function sigma3(n::Integer)
n == 0 && return zero(n)
s = zero(n)
d = one(n)
while d * d <= n
if n % d == 0
s += d * d * d
other = n ÷ d
if other != d
s += other * other * other
end
end
d += 1
end
return s
end
function hachimoji_letter(sigma3_value::Integer)
return UInt8(sigma3_value % 8)
end
function cartan_weight(a::Integer, b::Integer)
if a == b
return UInt64(273)
elseif a ÷ 2 == b ÷ 2
return UInt64(256)
else
return UInt64(0)
end
end
function angrysphinx_gate(elements::Vector{UInt64})
sum_counts = Dict{UInt64, Int}()
n = length(elements)
for i in 1:n
for j in i:n
s = elements[i] + elements[j]
sum_counts[s] = get(sum_counts, s, 0) + 1
end
end
collisions = 0
for (_, count) in sum_counts
if count > 1
collisions += count - 1
end
end
raw = 273 + 17 * collisions - 256 * collisions
energy_remaining = max(0, raw)
return (passed=collisions <= 1, collisions=collisions, energy_remaining=UInt64(energy_remaining))
end
function hachimoji_encode(n::Integer)
s3 = sigma3(UInt64(n))
letter = hachimoji_letter(s3)
return (sigma3_value=s3, hachimoji_letter=letter, letter_name=LETTER_NAMES[letter + 1])
end
function main()
println("=== Hachimoji Encoder + AngrySphinx Gate (Julia) ===")
println()
test_numbers = UInt64[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
expected_sigma3 = UInt64[1, 9, 28, 73, 126, 252, 344, 585, 757, 1134]
println("--- Sigma3 Verification ---")
all_sigma3_ok = true
for (i, n) in enumerate(test_numbers)
s3 = sigma3(n)
ok = s3 == expected_sigma3[i]
if !ok
all_sigma3_ok = false
end
status = ok ? "OK" : "FAIL"
@printf(" n=%2d: sigma3=%5d expected=%5d %s\n", n, s3, expected_sigma3[i], status)
end
@assert all_sigma3_ok "sigma3 verification failed"
println(" Sigma3: PASS")
println()
println("--- Hachimoji Encoding ---")
for n in test_numbers
r = hachimoji_encode(n)
@printf(" n=%2d: sigma3=%5d letter=%s (index=%d)\n",
n, r.sigma3_value, r.letter_name, r.hachimoji_letter)
end
println()
println("--- Cartan Weight ---")
@printf(" cartan_weight(0, 0) = %d (self)\n", cartan_weight(0, 0))
@printf(" cartan_weight(0, 1) = %d (same pair, opposite sign)\n", cartan_weight(0, 1))
@printf(" cartan_weight(0, 2) = %d (different pairs)\n", cartan_weight(0, 2))
println()
println("--- AngrySphinx Gate Tests ---")
r = angrysphinx_gate(UInt64[1, 2])
@assert r.passed == true
@assert r.collisions == 0
@assert r.energy_remaining == 273
@printf(" elements=[1,2] -> passed=%s, collisions=%d, energy=%d OK\n",
r.passed, r.collisions, r.energy_remaining)
r = angrysphinx_gate(UInt64[1, 2, 3])
@assert r.passed == true
@assert r.collisions == 1
@assert r.energy_remaining == 34
@printf(" elements=[1,2,3] -> passed=%s, collisions=%d, energy=%d OK\n",
r.passed, r.collisions, r.energy_remaining)
r = angrysphinx_gate(UInt64[1, 2, 3, 4])
@assert r.passed == false
@assert r.collisions == 3
@assert r.energy_remaining == 0
@printf(" elements=[1,2,3,4] -> passed=%s, collisions=%d, energy=%d OK\n",
r.passed, r.collisions, r.energy_remaining)
println(" AngrySphinx: PASS")
println()
println("--- E8LevelSet Tests ---")
r = angrysphinx_gate(UInt64[1, 2, 3])
@assert r.passed "E8LevelSet(32) gate should be OPEN"
@assert r.collisions == 1
@printf(" E8LevelSet(32): elements=[1,2,3] -> gate OPEN (%d collision)\n", r.collisions)
r = angrysphinx_gate(UInt64[1, 2, 3])
@assert r.passed "E8LevelSet(64) gate should be OPEN"
@assert r.collisions == 1
@printf(" E8LevelSet(64): elements=[1,2,3] -> gate OPEN (%d collision)\n", r.collisions)
r = angrysphinx_gate(UInt64[1, 2, 3, 4, 5])
@assert !r.passed "E8LevelSet(128) gate should be CLOSED"
@assert r.collisions == 6
@printf(" E8LevelSet(128): elements=[1,2,3,4,5] -> gate CLOSED (%d collisions)\n", r.collisions)
println(" E8LevelSet: PASS")
println()
println("=== All tests passed ===")
end
if !isinteractive()
main()
end