Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions nmcb/src/main/scala/examples/FriedmanNumber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand All @@ -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)
Expand All @@ -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]]]
Expand Down
6 changes: 5 additions & 1 deletion nmcb/src/test/scala/examples/TestFriedmanNumber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}