9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

マイナンバーのチェックデジットをScalaで計算する

Posted at

マイナンバーが届いたので、マイナンバーのチェックデジットを計算するを参考にscalaでマイナンバー検証コードを書いてみました。

def validateMyNumber(myNumber: String): Boolean = {

  if (myNumber.length != 12 || !myNumber.forall(_.isDigit)) {
    return false
  }

  val remainder =
    (1 to 11).map(i => myNumber(11 - i).asDigit * (if (i <= 6) i + 1 else i - 5)).sum % 11

  val checkDigit = myNumber.last.asDigit

  if (remainder <= 1) {
    checkDigit == 0
  } else {
    checkDigit == 11 - remainder
  }
}
結果
validateMyNumber("123456789010") // false
validateMyNumber("123456789011") // false
validateMyNumber("123456789012") // false
validateMyNumber("123456789013") // false
validateMyNumber("123456789014") // false
validateMyNumber("123456789015") // false
validateMyNumber("123456789016") // false
validateMyNumber("123456789017") // false
validateMyNumber("123456789018") // true
validateMyNumber("123456789019") // false

合ってそうなので、届いた自分のマイナンバーを入れてみたらちゃんとtrueが返ってきました。
よかったよかった。

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?