Problem:
Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003

Solution in Scala (2.11.7) (judge verdict: Accepted)

import java.util.Scanner

import scala.collection.mutable.ListBuffer

object PrimeInt {
  val ps: Stream[Int] = 2 #:: Stream.from(3).filter(i => ps.takeWhile(j => j * j <= i).forall(k => i % k > 0))

  def isPrime(n: Int): Boolean = ps.takeWhile(_ <= n).contains(n)
}

object Primes extends Iterable[Long] {
  var checked = 2L
  val xs = ListBuffer(2L)

  def contains(i: Long): Boolean = xs.takeWhile(_ <= i).contains(i)

  def apply(i: Int): Long = {
    while (xs.length <= i) {
      var n = checked + 1
      while (xs.takeWhile(j => j * j <= n).exists(n % _ == 0)) {
        n += 1
      }
      xs.append(n)
      checked = n
    }
    xs(i)
  }

  override def iterator: Iterator[Long] = new Iterator[Long] {
    var i = 0

    override def hasNext: Boolean = true

    override def next(): Long = {
      val x = apply(i); i += 1; x
    }
  }
}

object Main {
  def main(args: Array[String]) {
    val result = solve(new Scanner(System.in))
    if (result.length > 0) println(result)
  }

  def solve(in: String): String = solve(new Scanner(in))

  def prime(n: Int, primes: Seq[Int]) = primes.forall(p => n % p != 0)

  def solve(sc: Scanner): String = {
    val X = sc.nextInt

    var i = X
    while (!PrimeInt.isPrime(i)) i += 1

    i.toString
  }
}