blib/lib/Math/Goedel.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 8 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 94.3% 67 / 71 CC 8 SCAR 15.1
CC
8
Cov
96%
goedel
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.5
↑ Summary
1 package Math::Goedel;
2  
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 our @EXPORT_OK = qw/goedel/;
9  
10 #use Math::Prime::XS qw/is_prime/;
11 8 ▶use Math::Prime::Util qw/nth_prime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Math::BigInt try => q/GMP,Pari/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Math::Goedel - Fundamental Goedel number calculator
18  
19 =cut
20  
21 8 our $VERSION = '0.04';
22  
23 =head1 SYNOPSIS
24  
25 use Math::Goedel qw/goedel/;
26  
27 goedel(9); # 512 (2**9)
28 goedel(81); # 768 (2**8 * 3**1)
29 goedel(230);# 108 (2**2 * 3**3 * 5**0)
30  
31 Math::Goedel::enc(9); # same as goedel(9)
32  
33 goedel(9, offset => 1); # 1024 (2**(9+1))
34 goedel(81, reverse => 1); # 13112 (2**1 * 3**8)
35 goedel(4999, bigint => 1); # 24821251455656250000 as BigInt (2**1 * 3**9 * 5**9 * 7**9)
36  
37 =head1 DESCRIPTION
38  
39 Goedel number is calculated by following Goedel's encoding theorem
40  
41 enc(X0X1X2...Xn) = P0**X0 * P1**X1 * P2**X2 * ..... * Pn**Xn
42  
43 I<Xk> is a I<k> th digit (from left hand) of input number.
44  
45 I<Pk> is a I<k> th prime number.
46  
47 =head1 EXPORT
48  
49 @EXPORT_OK => qw/goedel/
50  
51 =head1 FUNCTIONS
52  
53 =head2 goedel($n, %opts)
54  
55 calculate goedel number for I<n>
56  
57 =head3 %opts
58  
59 =head4 offset => $i
60  
61 According to fundamental theorem, goedel numbers are not unique.
62  
63 goedel(23) == goedel(230); # 2**2 * 3**3 ( * 5**0 ) == 108
64  
65 To make it unique, you can specify I<offset> for I<Xk>
66  
67 enc(X0X1X2...Xn) = P0**(X0 +i) * P1**(X1 +i) * P2**(X2 +i) * ..... * Pn**(Xn +i)
68  
69 so,
70  
71 goedel(23, offset => 1); # 2**(2+1) * 3**(3+1) == 648
72 goedel(230, offset => 1); # 2**(2+1) * 3**(3+1) * 5**(0+1) == 3240
73  
74 =head4 reverse => 0|1
75  
76 This option is for same purpose as offset option.
77  
78 If reverse is set to 1, apply I<Xk> in reverse order,
79  
80 enc(X0X1X2...Xn) = P0**Xn * P1**Xn-1 * P2**Xn-2 * ..... * Pn**X0
81  
82 so,
83  
84 goedel(23, reverse => 1); # 2**3 * 3**2 == 72
85 goedel(230, reverse => 1); # 2**0 * 3**3 * 5**2 == 675
86  
87 =head4 bigint => 0|1
88  
89 This option is used to force result goedel numbers to be L<Math::BigInt>.
90 =cut
91  
92 48 ▶sub goedel {
Subroutine goedel: called CC=8 SCAR=20.8
Pod documented
93 48 my $n = shift;
94 48 my %opts = (
95 offset => 0,
96 reverse => 0,
97 bigint => 0,
98 @_ );
99  
100 48 ▶ croak "n should be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "" or $n =~ tr/0-9//c 3 45 48
Condition66%2/3
$n eq "" or $n =~ tr/0-9//c
l!l&&r!l&&!r
0345
MC/DC50%1/2
$n eq "" or $n =~ tr/0-9//c
$n eq "" $n =~ tr/0-9//c
Truth table66%2/3
A or B
A= $n eq ""B= $n =~ tr/0-9//c
ABresult
1X1
011
000
101 if $n eq '' || $n =~ tr/0123456789//c;
102  
103 45 my $offset = $opts{'offset'};
104 45 ▶ croak "offset should be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
if $offset eq "" or $offset =~ tr/0-9//c 3 42 45
Condition66%2/3
$offset eq "" or $offset =~ tr/0-9//c
l!l&&r!l&&!r
0342
MC/DC50%1/2
$offset eq "" or $offset =~ tr/0-9//c
$offset eq "" $offset =~ tr/0-9//c
Truth table66%2/3
A or B
A= $offset eq ""B= $offset =~ tr/0-9//c
ABresult
1X1
011
000
105 if $offset eq '' || $offset =~ tr/0123456789//c;
106  
107 42 my $one = do {
108 42 ▶ if ($opts{'bigint'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'bigint'}) { } 2 40 42
109 2 Math::BigInt->bone;
110 }
111 else {
112 40 $n - $n + 1;
113 }
114 };
115 42 my $g = $one;
116  
117 # from here, $n is treated just as string of digits
118 #$n = "$n";
119 42 ▶ $n = reverse $n if $opts{'reverse'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"reverse"} 8 34 42
120 42 foreach my $i (1 .. length($n)) {
121 127 $g *= ($one * nth_prime($i)) ** (substr($n, $i-1, 1)+$offset);
122 }
123 42 $g;
124 }
125  
126 =head2 enc($n)
127  
128 synonym for goedel($n). but it won't be exported.
129  
130 =cut
131  
132 8 ▶{ no strict q/vars/;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 8 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 8 *enc = *goedel;
135 }
136  
137 =head1 REFERENCES
138  
139 Goedel number: L<http://en.wikipedia.org/wiki/G%C3%B6del_number>
140  
141 Discussion of "how to make goedel number unique" (in Japanese):
142 L<http://ja.doukaku.org/comment/4657/>, L<http://ja.doukaku.org/comment/4661/>
143  
144 =head1 AUTHOR
145  
146 KATOU Akira (turugina), C<< <turugina at cpan.org> >>
147  
148 =head1 CONTRIBUTERS
149  
150 DANAJ
151  
152 =head1 BUGS
153  
154 Please report any bugs or feature requests to C<bug-math-goedel at rt.cpan.org>, or through
155 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Goedel>. I will be notified, and then you'll
156 automatically be notified of progress on your bug as I make changes.
157  
158  
159  
160  
161 =head1 SUPPORT
162  
163 You can find documentation for this module with the perldoc command.
164  
165 perldoc Math::Goedel
166  
167  
168 You can also look for information at:
169  
170 =over 4
171  
172 =item * RT: CPAN's request tracker
173  
174 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Goedel>
175  
176 =item * AnnoCPAN: Annotated CPAN documentation
177  
178 L<http://annocpan.org/dist/Math-Goedel>
179  
180 =item * CPAN Ratings
181  
182 L<http://cpanratings.perl.org/d/Math-Goedel>
183  
184 =item * Search CPAN
185  
186 L<http://search.cpan.org/dist/Math-Goedel>
187  
188 =back
189  
190  
191 =head1 ACKNOWLEDGEMENTS
192  
193  
194 =head1 COPYRIGHT & LICENSE
195  
196 Copyright 2008 KATOU Akira (turugina), all rights reserved.
197  
198 This program is free software; you can redistribute it and/or modify it
199 under the same terms as Perl itself.
200  
201  
202 =cut
203  
204 8 1; # End of Math::Goedel
↑ Summary