2019年の素数の日

興味本位で「2019年の素数の日」を計算してみました。笑
以下の19日が素数の日らしいです。

  • 1月
    • なし
  • 2月
    • 20190221 (2月21日)
    • 20190227 (2月27日)
  • 3月
    • 20190301 (3月1日)
    • 20190319 (3月19日)
    • 20190323 (3月23日)
  • 4月
    • 20190421 (4月21日)
  • 5月
    • 20190523 (5月23日)
    • 20190529 (5月29日)
  • 6月
    • 20190601 (6月1日)
    • 20190613 (6月13日)
  • 7月
    • 20190719 (7月19日)
  • 8月
    • 20190811 (8月11日)
    • 20190823 (8月23日)
  • 9月
    • 20190913 (9月13日)
  • 10月
    • 20191009 (10月9日)
    • 20191027 (10月27日)
  • 11月
    • 20191109 (11月9日)
    • 20191117 (11月17日)
  • 12月
    • 20191231 (12月31日)


書いたコードは以下です。言語は一応Kotlinです。

import java.time.LocalDate
import java.time.format.DateTimeFormatter

fun main() {
  val year      = 2019
  val beginDate = LocalDate.of(year,  1,  1)
  val formatter = DateTimeFormatter.ofPattern("yyyyMMdd")

  // 1年は最大でも366日
  for (i in 0 until 365) {
    val date    = beginDate.plusDays(i.toLong())
    val dateInt = date.format(formatter).toInt()
    
    // 翌年になった時点で終了
    if (date.year != year) break

    if (dateInt.isPrimeNumber()) println(dateInt)
  }

}

private fun Int.isPrimeNumber(): Boolean {
  // 素数は少なくとも2以上の整数
  for (i in 2 until this) {
    // 自身以外で割り切れたら素数ではない
    if (this % i == 0) return false
  }
  return true
}



たまにはこんな内容でもいいですよね。笑