多分源流
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);
}
}
}