fork download
  1. package eulerProject
  2.  
  3. /**
  4.  * Problem 251: Cardano Triplets.<br>i
  5.  * 20 June 2009<br>
  6.  * <br>
  7.  * A triplet of positive integers (a,b,c) is called a Cardano Triplet if it
  8.  * satisfies the condition:<br>
  9.  * cubeSquare(a + b * root(c)) + cubeSquare(a - b * root(c)) = 1<br>
  10.  * For example, (2,1,5) is a Cardano Triplet.<br>
  11.  * <br>
  12.  * There exist 149 Cardano Triplets for which a+b+c <= 1000.<br>
  13.  * <br>
  14.  * <b>Find how many Cardano Triplets exist such that a+b+c <= 110,000,000.</b><br>
  15.  * <br>
  16.  * Note: This problem has been changed recently, please check that you are
  17.  * using the right parameters.<br>
  18.  * <br>
  19.  * <hr>
  20.  * <pre>
  21.  * Solution:
  22.  * cbrt(a + b * sqrt(c)) + cbrt(a - b * sqrt(c)) = 1 <-->
  23.  * c = (8 * a ^ 3 + 15 * a ^ 2 + 6 * a - 1)/ (27 * b ^ 2) (Solved in Wolfram Alpha)
  24.  * Or:
  25.  * c = n / (27 * b ^ 2) (1)
  26.  * Where:
  27.  * n = 8 * a ^ 3 + 15 * a ^ 2 + 6 * a - 1 (2)
  28.  * For c > 1, from (1):
  29.  * b < sqrt(n / 27) (3)
  30.  * For:
  31.  * a + b + c <= L
  32.  *
  33.  * </pre>
  34.  */
  35. object Problem251a {
  36. import Utils._
  37.  
  38. type Cardano = (Int, Int, Int)
  39. type OptCardano = Option[Cardano]
  40.  
  41. def evaluateA(a: Int, max: Int): Int = {
  42.  
  43. val num = 8 * a * a * a + 15 * a * a + 6 * a - 1
  44.  
  45. def evaluateB(b: Int, qty: Int): Int = {
  46. if(b < 1) {
  47. qty
  48. } else {
  49. val (c, remC) = /%(num, 27 * b * b)
  50. if(remC == 0 && (a + b + c < max)) {
  51. log("%,11d + %,11d + %,11d = %,11d".format(a, b, c, (a + b + c)))
  52. evaluateB(b - 1, qty + 1)
  53. } else {
  54. evaluateB(b - 1, qty)
  55. }
  56. }
  57. }
  58.  
  59. val bMax = Math.sqrt(num / 27).toInt
  60. evaluateB(bMax, 0)
  61. }
  62.  
  63. def main(args : Array[String]) : Unit = {
  64. val max = 110000000
  65.  
  66. val t0 = System.currentTimeMillis
  67. val result = (1 to max).foldLeft(0)((sum, a) => sum + evaluateA(a, max))
  68. val deltaT = System.currentTimeMillis - t0
  69.  
  70. println("=" * 80)
  71. log(result)
  72. log("Total Time: " + deltaT + " ms")
  73. }
  74. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/opt/scala/bin/scalac: line 50: /dev/null: Permission denied
Main.scala:36: error: not found: value Utils
  import Utils._
         ^
Main.scala:49: error: not found: value /%
        val (c, remC) = /%(num, 27 * b * b)
                        ^
Main.scala:50: error: overloaded method value + with alternatives:
  (x: Double)Double <and>
  (x: Float)Float <and>
  (x: Long)Long <and>
  (x: Int)Int <and>
  (x: Char)Int <and>
  (x: Short)Int <and>
  (x: Byte)Int <and>
  (x: String)String
 cannot be applied to (Any)
        if(remC == 0 && (a + b + c < max)) {
                               ^
Main.scala:51: error: not found: value log
          log("%,11d + %,11d + %,11d = %,11d".format(a, b, c, (a + b + c)))
          ^
Main.scala:71: error: not found: value log
    log(result)
    ^
Main.scala:72: error: not found: value log
    log("Total Time: " + deltaT + " ms")
    ^
6 errors found
spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty