Last active
May 25, 2024 10:18
-
-
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/44c6e85919457f5d6827a6a6578e27ea55014885
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.4.2" | |
//> 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