Saturday, September 21, 2013

Problem 44 - Scala

I solved problem 44 in Scala, after just having freed it up with Prolog. This problem was relatively easy, so I didn't really need to bring in Scala, which is relatively one of the "big guns" (being a language that I am pretty familiar with), but I wanted to get another problem done and move as quickly as possible towards number 50. This solution very well may be replaced by a solution in a more difficult language soon.
object e44 {
  def min(x : Int, y : Int) : Int = if (x < y) x else y

  def isPent(p : Int) : Boolean = {
    val root =  math.sqrt(24 * p + 1).asInstanceOf[Int]
    return (root * root == 24*p + 1) && ((root + 1)% 6 == 0)
  }

  def pent(n : Int) : Int = n * (3 * n - 1) / 2

  def main(args: Array[String]) {
    var D = 1000000000
    var p1 = 0
    var p2 = 0
    for (i <- 1 to 10000) {
      for (j <- (i+1) to 10000) {
        p1 = pent(i)
        p2 = pent(j)
        if (isPent(p1 + p2) && isPent(p2 - p1)) {
          D = min(D, p2 - p1)
        }
      }
    }
    println(D)
  }
}

No comments:

Post a Comment