Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:10
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/56d1d09dfa9848f4d3f38998053f723f to your computer and use it in GitHub Desktop.
Save dacr/56d1d09dfa9848f4d3f38998053f723f to your computer and use it in GitHub Desktop.
Beal conjecture / conjecture de Beal. / published by https://github.com/dacr/code-examples-manager #cf918f8f-1587-4fca-b343-7ea0e5296b57/4a8944c5287e666ba6344cd0dad7298edc4a5449
// summary : Beal conjecture / conjecture de Beal.
// keywords : scala, math, conjecture, beal, @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 : cf918f8f-1587-4fca-b343-7ea0e5296b57
// created-on : 2019-08-14T14:43:09Z
// 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"
// ---------------------
/*
Beal conjecture wikipedia : https://en.wikipedia.org/wiki/Beal_conjecture
*/
import org.scalatest._,flatspec._,matchers._
import math._
// a^x + b^y = c^z x,y,z should be > 2
object Beal {
def commonFactors(a: Long, b: Long, c: Long) = {
val from = List(a, b, c)
(1L to max(a, max(b, c))).filter { x => from.forall {
_ % x == 0
}
}
}
// Take care as math.pow returns Double so precisions makes the formula wrongly works !
// @ pow(1,3)+pow(99,8) == pow(99,8)
// res1: Boolean = true
def exploreUsingLong(l1: Long, l2: Long): Unit = {
for {
a <- 1L to l1
b <- 1L to l1
c <- 1L to l1
x <- 3L to l2
aPx = pow(a, x)
y <- 3L to l2
bPy = pow(b, y)
z <- 3L to l2
cPz = pow(c, z)
if aPx + bPy == cPz
cf = commonFactors(a, b, c).filterNot(_ == 1L)
//if cf.size == 0 // if we want to find a counter example...
} {
println(s"""$a^$x + $b^$y = $c^$z\t\t${cf.mkString(",")}""")
}
}
// no precision issue with this implementation
def exploreUsingBigInt(l1: Int, l2: Int): Unit = {
for {
a <- 2 to l1
bigA = BigInt(a)
b <- 2 to l1
bigB = BigInt(b)
c <- 2 to l1
bigC = BigInt(c)
x <- 3 to l2
aPx = bigA.pow(x)
y <- 3 to l2
bPy = bigB.pow(y)
z <- 3 to l2
cPz = bigC.pow(z)
if aPx + bPy == cPz
cf = commonFactors(a, b, c).filterNot(_ == 1L)
//if cf.size == 0 // if we want to find a counter example...
} {
println(s"""$a^$x + $b^$y = $c^$z\t\t${cf.mkString(",")}""")
}
}
}
object BealConjectureTest extends AnyFlatSpec with should.Matchers {
"beal conjecture path" should "work" in {
succeed
}
}
//BealConjectureTest.execute()
def now(): Long = System.currentTimeMillis()
def howLongFor[T](proc : () => T): (T, Long) = {
val started = now()
val result:T = proc()
val duration = now() - started
(result, duration)
}
def printHowLongFor[T](message:String)(proc : () => T):T = {
val (result, duration) = howLongFor(proc)
println(s"$message : $duration ms")
result
}
printHowLongFor("Beal exploration using Long") { () =>
Beal.exploreUsingLong(50, 8)
}
printHowLongFor("Beal exploration using BigInt") { () =>
Beal.exploreUsingBigInt(50, 8)
}
//Beal.exploreUsingBigInt(200, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment