Last active
May 25, 2024 10:18
-
-
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/f7dc22bbea537567cf6e59e671913b74cf9f57d
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 : 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.4.2" | |
//> 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