-
Notifications
You must be signed in to change notification settings - Fork 1
/
viitenumero.pl
executable file
·57 lines (49 loc) · 1.5 KB
/
viitenumero.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
# viitenumero.pl
#
# (c) 2021 Alberto Garcia <[email protected]>
#
# This program calculates the Finnish reference number from a given
# natural number and returns it with the resulting ref. number and
# proper formatting.
#
# https://wiki.xmldation.com/support/fk/finnish_reference_number
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# http://www.gnu.org/licenses/gpl2.txt
use strict;
use warnings;
use bignum;
$ENV{LC_ALL} = "C";
$#ARGV == 0 or die "Usage: viitenumero.pl <number>\n";
($ARGV[0] =~ /^\d+$/) or die "Not a natural number $ARGV[0]\n";
my $refno = gen_ref_number("$ARGV[0]");
print "$refno\n";
exit 0;
sub gen_ref_number {
my $number = shift;
my $sum = 0;
my $i = 0;
my @factors = (7, 3, 1);
for (my $tmp = $number; $tmp > 0; $tmp = int($tmp / 10)) {
$sum += ($tmp % 10) * $factors[$i];
$i = ($i + 1) % 3;
}
$number .= ((10 - ($sum % 10)) % 10);
my $result = "";
while ($number =~ /^(\d+)(\d\d\d\d\d)$/) {
$result = "$2 $result";
$number = $1;
}
$result = "$number $result";
$result =~ s/ *$//;
return $result;
}