From 97f000a36938ef5c8e337b51048d5fe78271aab6 Mon Sep 17 00:00:00 2001 From: nmcb Date: Sat, 18 Jul 2026 10:11:08 +0200 Subject: [PATCH] cleanup friedman --- .../main/scala/examples/FriedmanNumber.scala | 22 +++++++++---------- .../scala/examples/TestFriedmanNumber.scala | 6 ++++- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/nmcb/src/main/scala/examples/FriedmanNumber.scala b/nmcb/src/main/scala/examples/FriedmanNumber.scala index a76a1c36..ad90e5e5 100644 --- a/nmcb/src/main/scala/examples/FriedmanNumber.scala +++ b/nmcb/src/main/scala/examples/FriedmanNumber.scala @@ -35,7 +35,7 @@ object FriedmanNumber: case object Concatenation extends Operation: def run(l: Int, r: Int): Option[Int] = - sys.error(s"unimplemented, special handling in computation") + sys.error(s"unimplemented, marker operation only which has special handling in computation order") object Operation: val all: Vector[Operation] = @@ -53,15 +53,15 @@ object FriedmanNumber: def isConcat: Boolean = this match - case BinaryOperation(l, r, Some(Concatenation)) => l.isConcat && r.isConcat - case Value(v) => true - case _ => false + case BinaryOperation(l, r, Concatenation) => l.isConcat && r.isConcat + case Value(v) => true + case _ => false def compute: Option[Int] = this.runtimeChecked match - case Value(Some(v)) => Some(v) - case BinaryOperation(l, r, Some(Concatenation)) => when(l.isConcat && r.isConcat)(s"${l.compute.get}${r.compute.get}".toInt) - case BinaryOperation(l, r, Some(o)) => l.compute.flatMap(lr => r.compute.flatMap(rr => o.run(lr, rr))) + case Value(v) => Some(v) + case BinaryOperation(l, r, Concatenation) => when(l.isConcat && r.isConcat)(s"${l.compute.get}${r.compute.get}".toInt) + case BinaryOperation(l, r, o) => l.compute.flatMap(lr => r.compute.flatMap(rr => o.run(lr, rr))) def bind(operations: Vector[Operation], arguments: Vector[Int]): ComputationOrder = val (result, _, _) = bindRecursively(operations, arguments) @@ -70,14 +70,14 @@ object FriedmanNumber: private def bindRecursively(operations: Vector[Operation], arguments: Vector[Int]): (ComputationOrder, Vector[Operation], Vector[Int]) = this match case Value(v) => - (Value(Some(arguments.head)), operations, arguments.tail) + (Value(arguments.head), operations, arguments.tail) case BinaryOperation(l, r, v) => val (fl, ol, al) = l.bindRecursively(operations.tail, arguments) val (fr, or, ar) = r.bindRecursively(ol, al) - (BinaryOperation(fl, fr, Some(operations.head)), or, ar) + (BinaryOperation(fl, fr, operations.head), or, ar) - case class Value(v: Option[Int] = None) extends ComputationOrder - case class BinaryOperation(l: ComputationOrder, r: ComputationOrder, o: Option[Operation] = None) extends ComputationOrder + case class Value(v: Int = 0) extends ComputationOrder + case class BinaryOperation(l: ComputationOrder, r: ComputationOrder, o: Operation = null) extends ComputationOrder private val possibleOperationsMemo = Memo.empty[Int, Vector[Vector[Operation]]] diff --git a/nmcb/src/test/scala/examples/TestFriedmanNumber.scala b/nmcb/src/test/scala/examples/TestFriedmanNumber.scala index 63df8210..77da3d41 100644 --- a/nmcb/src/test/scala/examples/TestFriedmanNumber.scala +++ b/nmcb/src/test/scala/examples/TestFriedmanNumber.scala @@ -8,7 +8,11 @@ class TestFriedmanNumber extends AnyFunSuite: test("FriedmanNumber") { assertResult( Vector( - 25, 121, 125, 126, 127, 128, 153, 216, 289, 343, 347, 625, 688, 736, 1022, 1024, 1206, 1255, 1260, 1285, 1296, 1395, 1435, 1503, 1530, 1792, 1827, 2048, 2187, 2349, 2500, 2501, 2502, 2503, 2504, 2505, 2506, 2507, 2508, 2509, 2592, 2737, 2916, 3125, 3159, 3281, 3375, 3378, 3685, 3784, 3864, 3972, 4088, 4096, 4106, 4167, 4536, 4624, 4628, 5120, 5776, 5832, 6144, 6145, 6455, 6880, 7928, 8092, 8192, 9025, 9216, 9261 + 25, 121, 125, 126, 127, 128, 153, 216, 289, 343, 347, 625, 688, 736, 1022, 1024, + 1206, 1255, 1260, 1285, 1296, 1395, 1435, 1503, 1530, 1792, 1827, 2048, 2187, 2349, + 2500, 2501, 2502, 2503, 2504, 2505, 2506, 2507, 2508, 2509, 2592, 2737, 2916, 3125, + 3159, 3281, 3375, 3378, 3685, 3784, 3864, 3972, 4088, 4096, 4106, 4167, 4536, 4624, + 4628, 5120, 5776, 5832, 6144, 6145, 6455, 6880, 7928, 8092, 8192, 9025, 9216, 9261 ) )(FriedmanNumber.sieve(10000)) }