File Coverage

ex/factors.pl
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 21 21 100.0


line stmt bran cond sub pod time code
1 1     1   2770 use v5.24;
  1         3  
2 1     1   4 use warnings;
  1         1  
  1         29  
3 1     1   3 use Test::More;
  1         3  
  1         7  
4 1     1   293 use Q::S::L qw(superpos fetch_matches);
  1         2  
  1         245  
5              
6             sub get_factors
7             {
8 3     3   5 my ($number) = @_;
9              
10             # produce all the possible factors
11 3         27 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     3   422 return fetch_matches { $possible_factors == ($number / $possible_factors) };
  3         20  
16             }
17              
18             my %numbers = (
19              
20             # number => factors
21             78 => [2, 3, 6, 13, 26, 39],
22             37 => [],
23             21 => [3, 7],
24             );
25              
26             while (my ($number, $factors) = each %numbers) {
27              
28             # this will be a superposition of all valid factors
29             my $factors_superposition = get_factors $number;
30              
31             # did we succeed?
32             foreach my $factor (@$factors) {
33             ok $factors_superposition == $factor, "factor $factor found ok";
34             }
35             is scalar $factors_superposition->states->@*, @$factors, "factors count ok";
36             }
37              
38             done_testing;
39              
40             __END__