結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー negistelnegistel
提出日時 2016-05-15 04:52:29
言語 Scala(Beta)
(3.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 891 bytes
コンパイル時間 6,325 ms
コンパイル使用メモリ 230,628 KB
最終ジャッジ日時 2023-09-12 00:39:11
合計ジャッジ時間 7,125 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
-- [E006] Not Found Error: Main.scala:12:22 ------------------------------------
12 |    val Array(l, h) = readLine.split(' ').map(_.toLong)
   |                      ^^^^^^^^
   |                      Not found: readLine
   |
   | longer explanation available when compiling with `-explain`
-- [E045] Cyclic Error: Main.scala:12:14 ---------------------------------------
12 |    val Array(l, h) = readLine.split(' ').map(_.toLong)
   |              ^
   |              Recursive value $1$ needs type
   |
   | longer explanation available when compiling with `-explain`
-- [E045] Cyclic Error: Main.scala:12:17 ---------------------------------------
12 |    val Array(l, h) = readLine.split(' ').map(_.toLong)
   |                 ^
   |                 Recursive value $1$ needs type
   |
   | longer explanation available when compiling with `-explain`
3 errors found

ソースコード

diff #

import scala.collection.immutable.IndexedSeq

object Main {
  def sieve(n: Int): IndexedSeq[Int] = {
    val isNotPrime = scala.collection.mutable.BitSet(n + 1)
    for (i <- 2 to n) if (!isNotPrime(i))
      for (j <- (i + i) to n by i) isNotPrime(j) = true
    return (2 to n).filter(!isNotPrime(_))
  }

  def main(args: Array[String]): Unit = {
    val Array(l, h) = readLine.split(' ').map(_.toLong)
    val primes = sieve(110000).reverse
    for (p <- primes.map(_.toLong)) {
      if (l <= p * p && p * p <= h) {
        for (q <- primes) if (l <= p * q && p * q <= h) {
          println(p * q)
          return
        }
      }
    }
    val factorList = new Array[Int]((h - l).toInt + 1)
    for (p <- primes)
      for (i <- (Array(l - l % p, p + p).max - l).toInt to (h - l).toInt by p)
        if(0 <= i) factorList(i) = p
    println(factorList.zipWithIndex.max._2 + l)
  }
}
0