fork download
  1. package eulerProject
  2.  
  3. /**
  4.  * Problem 221: Alexandrian Longegers<br>
  5.  * 13 December 2008<br>
  6.  * <br>
  7.  * We shall call a positive integer A an "Alexandrian integer", if there
  8.  * exist integers p, q, r such that:<br>
  9.  * <br>
  10.  * A = p * q * r and 1 / A = 1 / p + 1 / q + 1 / r <br>
  11.  * <br>
  12.  * For example, 630 is an Alexandrian integer (p = 5, q = -7, r = -18).
  13.  * In fact, 630 is the 6^(th) Alexandrian integer, the first 6 Alexandrian
  14.  * integers being: 6, 42, 120, 156, 420 and 630.<br>
  15.  * <br>
  16.  * <b>Find the 150000<sup>th</sup> Alexandrian integer.</b><br>
  17.  * <br>
  18.  * <hr>
  19.  * <pre>
  20.  * p * q * r = A (1)
  21.  * 1/p + 1/q + 1/r = 1/A (2)
  22.  * From (2):
  23.  * (q * r + p * r + p * q)/(p * q * r) = 1 / A (3)
  24.  * So
  25.  * p * q + p * r + q * r = p * (q + r) + q * r = p * s + q * r = 1 (4)
  26.  * Where
  27.  * s = q + r (5)
  28.  * From (1)
  29.  * q * r = A / p = d (6)
  30.  * Appling (6) in (4)
  31.  * p * s + d = 1 (7)
  32.  * s = (p - A) / p ^ 2 (8)
  33.  * How p > 0, A > 0 and A > p
  34.  * s < 0 (9)
  35.  * From (6), for (A / p) be a integer, p must be a divisor of A
  36.  *
  37.  * Para resumir
  38.  *
  39.  * -> p é divisor de A
  40.  * -> s = q + r = (p - A) / p^2, deve ser um inteiro negativo
  41.  * -> q e r são a solução da equação (s +- sqrt(s^2 - 4 * d)) / 2, onde d = A / p
  42.  * <pre>
  43.  */
  44. object Problem221a {
  45.  
  46. type Alexandrian = (Long, Long, Long, Long)
  47.  
  48. import Utils._
  49.  
  50. def getQR(s: Long, d: Long): Option[(Long, Long)] = {
  51. val delta = s * s - 4 * d
  52. var result: Option[(Long, Long)] = None
  53. if(delta > 0) {
  54. val rootDelta = getSqrt(delta)
  55. if(rootDelta.isRight) {
  56. val (q, remQ) = /%(s + rootDelta.right.get, 2)
  57. val (r, remR) = /%(s - rootDelta.right.get, 2)
  58. if(remQ == 0 && remR == 0) {
  59. result = Some((q, r))
  60. }
  61. }
  62. }
  63. result
  64. }
  65.  
  66. def testP(A: Long, p: Long): Option[Alexandrian] = {
  67. val (s, remainder) = /%(p - A, p * p)
  68. var result: Option[Alexandrian] = None
  69. if(remainder == 0) {
  70. val qr = getQR(s, A / p)
  71. if(qr.isDefined) {
  72. result = Some((A, p, qr.get._1, qr.get._2))
  73. }
  74. }
  75. result
  76. }
  77.  
  78. def evaluateA(A: Long, candidate: Long, limitCandidate: Long): Option[Alexandrian] = {
  79. if(candidate > limitCandidate) {
  80. None
  81. } else {
  82. val (quocient, remainder) = /%(A, candidate)
  83. if(remainder == 0) {
  84. val alex = testP(A, candidate)
  85. if(alex.isDefined) {
  86. alex
  87. } else {
  88. val alex1 = testP(A, quocient)
  89. if(alex1.isDefined) {
  90. alex1
  91. } else {
  92. evaluateA(A, candidate + 1, limitCandidate)
  93. }
  94. }
  95. } else {
  96. evaluateA(A, candidate + 1, limitCandidate)
  97. }
  98. }
  99. }
  100.  
  101. def getNthAlexandrian(quantity: Int): Long = {
  102.  
  103. def eval(A: Long, qty: Int): Long = {
  104. val alex = evaluateA(A, 1, Math.sqrt(A.toDouble).toLong)
  105. if(alex.isDefined) {
  106. val nextQty = qty + 1
  107. log("%,7d -> %,11d, %,7d, %,7d, %,7d".format(nextQty, alex.get._1, alex.get._2, alex.get._3, alex.get._4))
  108. if(nextQty == quantity) {
  109. A
  110. } else {
  111. eval(A + 1, nextQty)
  112. }
  113. } else {
  114. eval(A + 1, qty)
  115. }
  116. }
  117.  
  118. eval(1, 0)
  119. }
  120.  
  121. def main(args : Array[String]) : Unit = {
  122. val max = 1000
  123.  
  124. val t0 = System.currentTimeMillis
  125. val result = getNthAlexandrian(max)
  126. val deltaT = System.currentTimeMillis - t0
  127.  
  128. println("==============================")
  129. log(result)
  130. log("Total Time: " + deltaT + " ms")
  131. }
  132. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.scala:48: error: not found: value Utils
  import Utils._
         ^
Main.scala:54: error: not found: value getSqrt
      val rootDelta = getSqrt(delta)
                      ^
Main.scala:56: error: not found: value /%
        val (q, remQ) = /%(s + rootDelta.right.get, 2)
                        ^
Main.scala:57: error: not found: value /%
        val (r, remR) = /%(s - rootDelta.right.get, 2)
                        ^
Main.scala:59: error: type mismatch;
 found   : Any
 required: Long
          result = Some((q, r))
                         ^
Main.scala:59: error: type mismatch;
 found   : Any
 required: Long
          result = Some((q, r))
                            ^
Main.scala:67: error: not found: value /%
    val (s, remainder) = /%(p - A, p * p)
                         ^
Main.scala:70: error: type mismatch;
 found   : Any
 required: Long
      val qr = getQR(s, A / p)
                     ^
Main.scala:82: error: not found: value /%
      val (quocient, remainder) = /%(A, candidate)
                                  ^
Main.scala:88: error: type mismatch;
 found   : Any
 required: Long
          val alex1 = testP(A, quocient)
                               ^
Main.scala:107: error: not found: value log
        log("%,7d -> %,11d, %,7d, %,7d, %,7d".format(nextQty, alex.get._1, alex.get._2, alex.get._3, alex.get._4))
        ^
Main.scala:129: error: not found: value log
    log(result)
    ^
Main.scala:130: error: not found: value log
    log("Total Time: " + deltaT + " ms")
    ^
13 errors found
Usage: scalac <options> <source files>
where possible standard options include:
  -Dproperty=value                Pass -Dproperty=value directly to the runtime system.
  -J<flag>                        Pass <flag> directly to the runtime system.
  -P:<plugin>:<opt>               Pass an option to a plugin
  -X                              Print a synopsis of advanced options.
  -bootclasspath <path>           Override location of bootstrap class files.
  -classpath <path>               Specify where to find user class files.
  -d <directory|jar>              destination for generated classfiles.
  -dependencyfile <file>          Set dependency tracking file.
  -deprecation                    Emit warning and location for usages of deprecated APIs.
  -encoding <encoding>            Specify character encoding used by source files.
  -explaintypes                   Explain type errors in more detail.
  -extdirs <path>                 Override location of installed extensions.
  -feature                        Emit warning and location for usages of features that should be imported explicitly.
  -g:<level>                      Set level of generated debugging info. (none,source,line,vars,notailcalls) default:vars
  -help                           Print a synopsis of standard options
  -javabootclasspath <path>       Override java boot classpath.
  -javaextdirs <path>             Override java extdirs classpath.
  -language:<_,feature,-feature>  Enable or disable language features: `_' for all, `-language:help' to list
  -no-specialization              Ignore @specialize annotations.
  -nobootcp                       Do not use the boot classpath for the scala jars.
  -nowarn                         Generate no warnings.
  -optimise                       Generates faster bytecode by applying optimisations to the program
  -print                          Print program with Scala-specific features removed.
  -sourcepath <path>              Specify location(s) of source files.
  -target:<target>                Target platform for object files. All JVM 1.5 targets are deprecated. (jvm-1.5,jvm-1.6,jvm-1.7,jvm-1.8) default:jvm-1.6
  -toolcp <path>                  Add to the runner classpath.
  -unchecked                      Enable additional warnings where generated code depends on assumptions.
  -uniqid                         Uniquely tag all identifiers in debugging output.
  -usejavacp                      Utilize the java.class.path in classpath resolution.
  -usemanifestcp                  Utilize the manifest in classpath resolution.
  -verbose                        Output messages about what the compiler is doing.
  -version                        Print product version and exit.
  @<file>                         A text file containing compiler arguments (options and source files)

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