2
1

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.

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

Last updated at Posted at 2016-11-30

マイナンバーのチェックデジットを計算する(Ruby版)を参考に、練習も兼ねてAppleScriptでコードを書きました。

動作環境

macOS Sierra 10.12

/Applications/Utilities/Script Editor.appをおもむろに開き、以下のソースを貼り付けて⌘+R

ソースコード

validMyNumber.scpt
on validateMyNumber(myNumber)
	
	-- 数値にキャストできるか
	try
		myNumber as number
	on error
		return false
	end try
	
	-- 12桁か
	if (count of myNumber) is not 12 then
		return false
	end if
	
	-- チェックデジットを分離
	set checkDigit to end of characters of myNumber as integer
	set digits to reverse of characters of text 1 thru -2 of myNumber
	
	-- 数列の和を11で割った余りを計算
	set theSum to 0
	set n to 1
	repeat with digit in digits
		set p to digit as integer
		if n  6 then
			set q to n + 1
		else
			set q to n - 5
		end if
		set theSum to theSum + (p * q)
		set n to n + 1
	end repeat
	
	-- check digit
	set remainder to theSum mod 11
	if remainder  1 then
		return checkDigit = 0
	else
		return checkDigit = (11 - remainder)
	end if
	
end validateMyNumber

log validateMyNumber("123456789010") -- false
log validateMyNumber("123456789011") -- false
log validateMyNumber("123456789012") -- false
log validateMyNumber("123456789013") -- false
log validateMyNumber("123456789014") -- false
log validateMyNumber("123456789015") -- false
log validateMyNumber("123456789016") -- false
log validateMyNumber("123456789017") -- false
log validateMyNumber("123456789018") -- true
log validateMyNumber("123456789019") -- false

追記

@Piyomaruさんよりいただいた、

リスト中の変数「sum」は、よく使われているスクリプティング機能拡張(OSAX)で使いがちな英単語なので、「aSum」とか「theSum」といった英単語にした方が安全です。

との指摘を元に修正しました!

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?