ex/factors.pl

filter: Statementstmt 100% 22 / 22 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 30 / 30 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ex/dice-roll.pl ↑ Summary ex/prime.pl »
1 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Q::S::L qw(superpos fetch_matches);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub get_factors
7 3 ▶{
Subroutine get_factors: called CC=1 SCAR=0.0
8 3 my ($number) = @_;
9  
10 # produce all the possible factors
11 3 my $possible_factors = superpos(2 .. $number / 2);
12  
13 # for every state, get those that match a condition
14 # (any possible factor that is present after the number is divided by any other one)
15 3 ▶ return fetch_matches { $possible_factors == ($number / $possible_factors) };
Subroutine __ANON__: called CC=1 SCAR=0.0
16 }
17  
18 1 my %numbers = (
19  
20 # number => factors
21 78 => [2, 3, 6, 13, 26, 39],
22 37 => [],
23 21 => [3, 7],
24 );
25  
26 1 ▶while (my ($number, $factors) = each %numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %numbers) 3 1 4
27  
28 # this will be a superposition of all valid factors
29 3 my $factors_superposition = get_factors $number;
30  
31 # did we succeed?
32 3 foreach my $factor (@$factors) {
33 8 ok $factors_superposition == $factor, "factor $factor found ok";
34 }
35 3 is scalar $factors_superposition->states->@*, @$factors, "factors count ok";
36 }
37  
38 1 done_testing;
39  
40 __END__
« ex/dice-roll.pl ↑ Summary ex/prime.pl »