#!/usr/bin/perl -w
# taiwanpostofficechecksum -- 計算台灣郵局局號與帳號檢號
# Compute Taiwan Postal account etc. number checksums
# Copyright       : http://www.fsf.org/copyleft/gpl.html
# Author          : 積丹尼 Dan Jacobson -- http://jidanni.org/comp/checksums/
# Created On      : 6/2001
# Last Modified On: Fri Sep 13 16:37:52 2013
# Update Count    : 54

# 輸入 Input: 局號或帳號（儲金或劃撥等）一行一個
# one post office or account number per input line
# 使用例子 Example: $ echo 002154 | taiwanpostofficechecksum
# 002154-6

# 運演算法則由郵政儲金匯業局電子處理資料中心提供給我：
# Algorithm provided to me by the post office:
# D1 D2 D3 D4 D5 D6 [D7]
# Y = 2*D1 + 3*D2 + 4*D3 + 5*D4 + 6*D5 + 7*D6 [+ 8*D7]
# X = 11 – MOD(Y,11)
# PRINT MOD(X,10)

use strict;
use warnings FATAL => 'all';
while (<>) {
    chomp;
    die "輸入錯誤 Invalid input: \"$_\"" unless (/^\d{6,7}$/);
    my $multiplier = 2;
    my $total      = 0;
    for ( split '' ) { $total += $multiplier++ * $_ }
    printf "$_-%d\n", ( 11 - $total % 11 ) % 10;
}

# 我發現 1 出現率為 18%，其他 2-9 則 9%!
# The checksum 1 occurs 18% of the time, the other digits occur 9%!
