4
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.

WindowsPowerShell & C#マイナンバーのチェックデジットを計算する

Last updated at Posted at 2015-10-31

多分源流

Ruby - マイナンバーのチェックデジットを計算する - Qiita

やってみましょう

PowerShell

Check-MyNumber.ps1
filter Get-CalcResult{
    if($_ -le 6 ){
        return [int]$digits[$digits.Length - 2 - $_] * ($_ + 1)
    }else{
        return [int]$digits[$digits.Length - 2 - $_] * ($_ - 5)
    }
}

function Check-MyNumber{
    param(
        [String] $MyNumber
    )
    if($MyNumber.Length -ne 12){
        throw New-Object ArgumentException("マイナンバーは12桁です")
    }

    $digits = $MyNumber -split "" 

    $checkDigit = $digits[$digits.Length - 2];

    $sum = 0
    (1..11) | Get-CalcResult | %{$sum += $_}

    $reminder = $sum % 11
    
    if($reminder -le 1 ){
        return $checkDigit -eq 0
    }else{
        return $checkDigit -eq (11 - $reminder)
    }
}

PS C:\> Check-MyNumber 123456789010
False
PS C:\> Check-MyNumber 123456789011
False
PS C:\> Check-MyNumber 123456789012
False
PS C:\> Check-MyNumber 123456789013
False
PS C:\> Check-MyNumber 123456789014
False
PS C:\> Check-MyNumber 123456789015
False
PS C:\> Check-MyNumber 123456789016
False
PS C:\> Check-MyNumber 123456789017
False
PS C:\> Check-MyNumber 123456789018
True
PS C:\> Check-MyNumber 123456789019
False
PS C:\> Check-MyNumber 023456789013
True

C#

既にやられている方いますが
C#でマイナンバーのチェックデジットを計算する - Qiita

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ValidateMyNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine(ValidateMyNumber("123456789010")); //=>False
            WriteLine(ValidateMyNumber("123456789011")); //=>False
            WriteLine(ValidateMyNumber("123456789012")); //=>False
            WriteLine(ValidateMyNumber("123456789013")); //=>False
            WriteLine(ValidateMyNumber("123456789014")); //=>False
            WriteLine(ValidateMyNumber("123456789015")); //=>False
            WriteLine(ValidateMyNumber("123456789016")); //=>False
            WriteLine(ValidateMyNumber("123456789017")); //=>False
            WriteLine(ValidateMyNumber("123456789018")); //=>True
            WriteLine(ValidateMyNumber("123456789019")); //=>False
            WriteLine(ValidateMyNumber("023456789013")); //=>True

            ReadLine();
        }

        public static bool ValidateMyNumber(string mynumber)
        {
            if (mynumber.Length != 12)
                throw new ArgumentException("マイナンバーは12桁です");

            var digits = mynumber.Select(x => int.Parse(x.ToString())).ToArray();

            var checkDigit = digits.Last();

            var reminder = Enumerable.Range(1, 11)
                .Select(n => digits[digits.Length - 1 - n] * (n <= 6 ? n + 1 : n - 5))
                .Sum() % 11;
            return checkDigit == (reminder <= 1 ? 0 : 11 - reminder);
        }
    }
}

4
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
4
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?