blib/lib/Math/Counting.pm

filter: Statementstmt 100% 72 / 72 Branchbran 100% 16 / 16 Conditioncond 80.9% 17 / 21 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 94.2% 130 / 138 CC 17 SCAR 13.4
CC
17
Cov
96%
permutation
6.0
combination
6.0
factorial
4.0
CRAP 3.8
↑ Summary
1 package Math::Counting;
2 4 our $AUTHORITY = 'cpan:GENE';
3 # ABSTRACT: Combinatorial counting operations
4  
5 4 our $VERSION = '0.1307';
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Export either "student" or "engineering" methods.
11 4 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 our %EXPORT_TAGS = (
13 student => [qw( factorial permutation combination )],
14 big => [qw( bfact bperm bcomb bderange )],
15 );
16 4 our @EXPORT_OK = qw(
17 factorial permutation combination
18 bfact bperm bcomb
19 bderange
20 );
21 4 our @EXPORT = ();
22  
23 # Try to use a math processor.
24 4 ▶use Math::BigFloat try => 'GMP,Pari'; # Used for derangement computation only.
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Math::BigInt try => 'GMP,Pari';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 8 ▶sub factorial {
Subroutine factorial: called CC=4 SCAR=13.9
Pod documented
29 8 my $n = shift;
30 8 ▶ return unless defined $n && $n =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /^\d+$/ 2 6 8
Condition100%3/3
defined $n and $n =~ /^\d+$/
!ll&&!rl&&r
116
MC/DC100%2/2
defined $n and $n =~ /^\d+$/
defined $n $n =~ /^\d+$/
Truth table100%3/3
A and B
A= defined $nB= $n =~ /^\d+$/
ABresult
0X0
100
111
31 6 my $product = 1;
32 6 ▶ while( $n > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 243 6 249
33 243 $product *= $n--;
34 }
35 6 return $product;
36 }
37  
38  
39 7 ▶sub bfact {
Subroutine bfact: called CC=1 SCAR=0.0
Pod documented
40 7 my $n = shift;
41 7 $n = Math::BigInt->new($n);
42 7 return $n->bfac;
43 }
44  
45  
46 10 ▶sub permutation {
Subroutine permutation: called CC=6 SCAR=18.0
Pod documented
47 10 my( $n, $k ) = @_;
48 10 ▶ return unless defined $n && $n =~ /^\d+$/ && defined $k && $k =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /^\d+$/ and defined $k and $k =~ /^\d+$/ 2 8 10
Condition77%7/9
defined $n and $n =~ /^\d+$/ and defined $k and $k =~ /^\d+$/
!ll&&!rl&&r
118
defined $n and $n =~ /^\d+$/ and defined $k
!ll&&!rl&&r
109
defined $n and $n =~ /^\d+$/
!ll&&!rl&&r
019
MC/DC50%2/4
defined $n and $n =~ /^\d+$/ and defined $k and $k =~ /^\d+$/
defined $n $n =~ /^\d+$/ defined $k $k =~ /^\d+$/
Truth table60%3/5
A and B and C and D
A= defined $nB= $n =~ /^\d+$/C= defined $kD= $k =~ /^\d+$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
49 8 my $product = 1;
50 8 ▶ while( $k > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k > 0) 140 8 148
51 140 $product *= $n--;
52 140 $k--;
53 }
54 8 return $product;
55 }
56  
57  
58 20 ▶sub bperm {
Subroutine bperm: called CC=2 SCAR=6.9
Pod documented
59 20 my( $n, $k, $r ) = @_;
60 20 $n = Math::BigInt->new($n);
61 20 $k = Math::BigInt->new($k);
62 # With repetitions?
63 20 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 10 10 20
64 10 return $n->bpow($k);
65 }
66 else {
67 10 $k = $n - $k;
68 10 return $n->bfac / $k->bfac;
69 }
70 }
71  
72  
73 7 ▶sub bderange {
Subroutine bderange: called CC=2 SCAR=6.9
Pod documented
74 7 my $n = shift;
75 7 my $mone = Math::BigFloat->bone('-'); # -1
76 7 my $s = Math::BigFloat->bzero;
77 7 for ( 0 .. $n ) {
78 59 my $i = Math::BigFloat->new($_);
79 59 my $m = $mone->copy;
80 59 my $j = $m->bpow($i);
81 59 my $x = $i->copy;
82 59 my $f = $x->bfac;
83 59 $s += $j / $f;
84 }
85 7 $n = Math::BigFloat->new($n);
86 7 return $n->bfac * $s;
87 }
88  
89  
90 11 ▶sub combination {
Subroutine combination: called CC=6 SCAR=18.0
Pod documented
91 11 my( $n, $k ) = @_;
92 11 ▶ return unless defined $n && $n =~ /^\d+$/ && defined $k && $k =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /^\d+$/ and defined $k and $k =~ /^\d+$/ 3 8 11
Condition77%7/9
defined $n and $n =~ /^\d+$/ and defined $k and $k =~ /^\d+$/
!ll&&!rl&&r
218
defined $n and $n =~ /^\d+$/ and defined $k
!ll&&!rl&&r
209
defined $n and $n =~ /^\d+$/
!ll&&!rl&&r
029
MC/DC50%2/4
defined $n and $n =~ /^\d+$/ and defined $k and $k =~ /^\d+$/
defined $n $n =~ /^\d+$/ defined $k $k =~ /^\d+$/
Truth table60%3/5
A and B and C and D
A= defined $nB= $n =~ /^\d+$/C= defined $kD= $k =~ /^\d+$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
93 8 my $product = 1;
94 8 ▶ while( $k > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k > 0) 140 8 148
95 140 $product *= $n--;
96 140 $product /= $k--;
97 }
98 8 return $product;
99 }
100  
101  
102 22 ▶sub bcomb {
Subroutine bcomb: called CC=2 SCAR=6.9
Pod documented
103 22 my( $n, $k, $r ) = @_;
104 22 $n = Math::BigInt->new($n);
105 22 $k = Math::BigInt->new($k);
106 # With repetitions?
107 22 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 11 11 22
108 11 my $c1 = $n + $k - 1;
109 11 my $c2 = $n - 1;
110 11 return $c1->bfac / ($k->bfac * $c2->bfac);
111 }
112 else {
113 11 my $c1 = $n - $k;
114 11 return $n->bfac / ($k->bfac * $c1->bfac);
115 }
116 }
117  
118 4 1;
119  
120 __END__
↑ Summary