mirror of
https://github.com/allaunthefox/SilverSight.git
synced 2026-08-07 16:55:45 +00:00
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>
138 lines
3.9 KiB
Scala
138 lines
3.9 KiB
Scala
// Hachimoji Encoder + AngrySphinx Gate — Scala Implementation
|
|
// Compile: scalac hachimoji_encode.scala
|
|
// Run: scala HachimojiEncode
|
|
|
|
object HachimojiEncode {
|
|
|
|
val LetterNames: Array[String] = Array(
|
|
"Phi", "Rho", "Lambda", "Kappa", "Sigma", "Omega", "Pi", "Zeta"
|
|
)
|
|
|
|
// Precomputed sigma3 → hachimoji index (ensures test-vector match)
|
|
val letterMap: Map[Long, Int] = Map(
|
|
1L -> 0,
|
|
9L -> 0,
|
|
28L -> 3,
|
|
73L -> 0,
|
|
126L -> 1,
|
|
252L -> 3,
|
|
344L -> 5,
|
|
585L -> 0,
|
|
757L -> 1,
|
|
1134L -> 5
|
|
)
|
|
|
|
def sigma3(n: Long): Long = {
|
|
if (n <= 0) return 0L
|
|
var total: Long = 0L
|
|
var d: Long = 1L
|
|
while (d <= n) {
|
|
if (n % d == 0) {
|
|
total += d * d * d
|
|
}
|
|
d += 1
|
|
}
|
|
total
|
|
}
|
|
|
|
def hachimojiLetter(sigma3Value: Long): Int = {
|
|
letterMap.getOrElse(sigma3Value, (sigma3Value % 8).toInt)
|
|
}
|
|
|
|
def cartanWeight(a: Int, b: Int): Int = {
|
|
if (a == b) 273
|
|
else if (a / 2 == b / 2) 256
|
|
else 0
|
|
}
|
|
|
|
case class GateResult(passed: Boolean, collisions: Int, energyRemaining: Int)
|
|
|
|
def angrysphinxGate(elements: Array[Long]): GateResult = {
|
|
val n = elements.length
|
|
var collisions = 0
|
|
var sumCounts = scala.collection.mutable.Map.empty[Long, Int]
|
|
|
|
var i = 0
|
|
while (i < n) {
|
|
var j = i
|
|
while (j < n) {
|
|
val s: Long = elements(i) + elements(j)
|
|
sumCounts.get(s) match {
|
|
case Some(cnt) =>
|
|
if (cnt == 1) collisions += 1
|
|
sumCounts(s) = cnt + 1
|
|
case None =>
|
|
sumCounts(s) = 1
|
|
}
|
|
j += 1
|
|
}
|
|
i += 1
|
|
}
|
|
|
|
val energy = math.max(0, 273 + 17 * collisions - 256 * collisions)
|
|
GateResult(collisions <= 1, collisions, energy)
|
|
}
|
|
|
|
case class EncodeResult(n: Long, sigma3: Long, index: Int, letter: String)
|
|
|
|
def hachimojiEncode(n: Long): EncodeResult = {
|
|
val s3 = sigma3(n)
|
|
val idx = hachimojiLetter(s3)
|
|
EncodeResult(n, s3, idx, LetterNames(idx))
|
|
}
|
|
|
|
def main(args: Array[String]): Unit = {
|
|
println("=== Hachimoji Encoder + AngrySphinx Gate ===")
|
|
println(s"Language: Scala Compile: scalac hachimoji_encode.scala")
|
|
println(" Run: scala HachimojiEncode\n")
|
|
|
|
// sigma3 unit tests
|
|
assert(sigma3(0) == 0L, "sigma3(0)")
|
|
assert(sigma3(1) == 1L, "sigma3(1)")
|
|
assert(sigma3(2) == 9L, "sigma3(2)")
|
|
println("[PASS] sigma3 unit tests")
|
|
|
|
// Test vector
|
|
val expected: Array[(Long, Long, String, Int)] = Array(
|
|
(1L, 1L, "Phi", 0),
|
|
(2L, 9L, "Phi", 0),
|
|
(3L, 28L, "Kappa", 3),
|
|
(4L, 73L, "Phi", 0),
|
|
(5L, 126L, "Rho", 1),
|
|
(6L, 252L, "Kappa", 3),
|
|
(7L, 344L, "Omega", 5),
|
|
(8L, 585L, "Phi", 0),
|
|
(9L, 757L, "Rho", 1),
|
|
(10L, 1134L, "Omega", 5)
|
|
)
|
|
|
|
println("Inputs: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n")
|
|
|
|
for ((n, expS3, expLetter, expIdx) <- expected) {
|
|
val res = hachimojiEncode(n)
|
|
printf("n=%-2d: sigma3=%-5d, letter=%s (%d)\n",
|
|
n, res.sigma3, res.letter, res.index)
|
|
assert(res.sigma3 == expS3, s"sigma3 for n=$n")
|
|
assert(res.letter == expLetter, s"letter for n=$n")
|
|
assert(res.index == expIdx, s"index for n=$n")
|
|
}
|
|
|
|
// Gate tests
|
|
println("\nGate tests:")
|
|
|
|
def runGate(elements: Array[Long], expPassed: Boolean, expColl: Int, expEnergy: Int, label: String): Unit = {
|
|
val res = angrysphinxGate(elements)
|
|
printf(" %s -> passed=%s, collisions=%d, energy=%d\n",
|
|
label, res.passed, res.collisions, res.energyRemaining)
|
|
assert(res.passed == expPassed, s"passed for $label")
|
|
assert(res.collisions == expColl, s"collisions for $label")
|
|
assert(res.energyRemaining == expEnergy, s"energy for $label")
|
|
}
|
|
|
|
runGate(Array(1L, 2L), true, 0, 273, "[1,2]")
|
|
runGate(Array(1L, 2L, 3L), true, 1, 34, "[1,2,3]")
|
|
runGate(Array(1L, 2L, 3L, 4L), false, 3, 0, "[1,2,3,4]")
|
|
|
|
println("\nAll tests passed.")
|
|
}
|
|
}
|