Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/0218808afc53b3a3cf702e892f34ba86 to your computer and use it in GitHub Desktop.
Save dacr/0218808afc53b3a3cf702e892f34ba86 to your computer and use it in GitHub Desktop.
Numbers which can be computed with the same figure / published by https://github.com/dacr/code-examples-manager #395ba7b7-e618-4dbd-9bbf-3681c61a1762/9a7bf2f6fc79064ae096772880c6709062046a16
// summary : Numbers which can be computed with the same figure
// keywords : scala, math, challenge, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 395ba7b7-e618-4dbd-9bbf-3681c61a1762
// created-on : 2020-03-04T21:12:24Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.1.1"
//> using dep "org.scalatest::scalatest:3.2.10"
// ---------------------
import org.scalatest._,flatspec._,matchers._
import scala.math._
type Solution=(Double,String)
//def fact(n:Long):Long = {
// if (n==0L) 1L
// else n*fact(n-1L)
//}
def fact(n:Long):Long = {
@annotation.tailrec
def worker(cn:Long, acc:Long):Long = {
if (cn==0) acc
else worker(cn-1, acc*cn)
}
worker(n, 1L)
}
/**
* Find all computable numbers using the same digit the given number of time.
*
* Usable operations are : !, /, +, -, *, ^
* @param figure which figure to use for all computes
* @param count how many figure we can use
* @return found solutions
*/
def searchComputableFrom(figure: Int, count: Int): Set[Solution] = {
val maxDepth=10
def worker(currentValue: Double, currentCount: Int, proof:String, depth:Int): Set[Solution] = {
val newdepth=depth+1
if (currentCount==count) Set(currentValue->proof)
else if (currentCount>count || depth>=maxDepth) Set()
else if (currentCount==0) {
worker(figure, currentCount+1, s"$figure",newdepth)++
worker(fact(figure), currentCount+1, s"$figure!",newdepth)
} else {
worker(currentValue+figure, currentCount+1, s"$proof+$figure",newdepth)++
worker(currentValue-figure, currentCount+1, s"$proof-$figure",newdepth)++
worker(currentValue*figure, currentCount+1, s"($proof)*$figure",newdepth)++
worker(currentValue/figure, currentCount+1, s"($proof)/$figure",newdepth)++
worker(figure/currentValue, currentCount+1, s"$figure/($proof)",newdepth)++
worker(pow(currentValue, figure), currentCount+1, s"($proof)^$figure",newdepth)++
worker(pow(figure,currentValue), currentCount+1, s"$figure^($proof)",newdepth)++
worker(fact(currentValue.toLong), currentCount, s"($proof)!",newdepth)
}
}
worker(0, 0, "",0)
}
object MathChallengeTest extends AnyFlatSpec with should.Matchers {
"searchComputableFrom" should "return the right results" in {
searchComputableFrom(1, 1) shouldBe Set(
1 -> "1!"
)
searchComputableFrom(1, 2) shouldBe Set(
0 -> "1!-1!",
0 -> "1-1",
0 -> "1!-1",
0 -> "1-1!",
1 -> "1^1",
1 -> "1/1",
1 -> "1*1",
2 -> "1!+1!",
2 -> "1+1",
2 -> "1!+1",
2 -> "1+1!",
)
}
}
MathChallengeTest.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment