[libmath-prime-util-perl] 11/16: Add tests for all_factors
Partha P. Mukherjee
ppm-guest at moszumanska.debian.org
Thu May 21 18:44:35 UTC 2015
This is an automated email from the git hooks/post-receive script.
ppm-guest pushed a commit to annotated tag v0.08
in repository libmath-prime-util-perl.
commit 78d5bedb51da4fee677063e26a520114173908d5
Author: Dana Jacobsen <dana at acm.org>
Date: Wed Jun 20 19:58:50 2012 -0600
Add tests for all_factors
---
Changes | 2 +-
TODO | 6 ------
lib/Math/Prime/Util.pm | 8 +++++---
t/50-factoring.t | 33 +++++++++++++++++++++++++++++++--
4 files changed, 37 insertions(+), 12 deletions(-)
diff --git a/Changes b/Changes
index 051b029..5e4f3f4 100644
--- a/Changes
+++ b/Changes
@@ -7,7 +7,7 @@ Revision history for Perl extension Math::Prime::Util.
performance comparisons.
- Static presieve for 7, 11, and 13. 1k of ROM used for prefilling sieve
memory, meaning we can skip the 7, 11, and 13 loops. ~15% speedup.
- - Add all_factors function.
+ - Add all_factors function and added tests to t/50-factoring.t.
0.07 17 June 2012
- Fixed a bug in next_prime found by Lou Godio (thank you VERY much!).
diff --git a/TODO b/TODO
index 65dc20d..8369292 100644
--- a/TODO
+++ b/TODO
@@ -22,12 +22,6 @@
- better prime count upper/lower bounds
-- Thread-safety. Change all uses of sieve and segment to include a free call.
- For segment we can:
- 1) mutex (holy slowdown, batman)
- 2) use one cache, and malloc/free if the cache isn't available.
- For sieve I think we'll need a counting semaphore.
-
- Move .c / .h files into separate directory.
version does it in a painful way. Something simpler to be had?
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index e0e4300..dceb2ff 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -185,6 +185,7 @@ sub all_factors {
my @factors = factor($n);
my %all_factors;
foreach my $f1 (@factors) {
+ next if $f1 >= $n;
my @all = keys %all_factors;;
foreach my $f2 (@all) {
$all_factors{$f1*$f2} = 1 if ($f1*$f2) < $n;
@@ -622,9 +623,10 @@ process is repeated for each non-prime factor.
my @divisors = all_factors(30); # returns (2, 3, 5, 6, 10, 15)
-Produces all the divisors of a positive number input. Note that 1 and the
-input number are excluded. The divisors are a power set of multiplications
-of the prime factors, returned as a uniqued sorted list.
+Produces all the divisors of a positive number input. 1 and the input number
+are excluded (which implies that an empty list is returned for any prime
+number input). The divisors are a power set of multiplications of the prime
+factors, returned as a uniqued sorted list.
=head2 trial_factor
diff --git a/t/50-factoring.t b/t/50-factoring.t
index 1868ede..d6fe13d 100644
--- a/t/50-factoring.t
+++ b/t/50-factoring.t
@@ -3,7 +3,7 @@ use strict;
use warnings;
use Test::More;
-use Math::Prime::Util qw/factor is_prime/;
+use Math::Prime::Util qw/factor all_factors is_prime/;
my $use64 = Math::Prime::Util::_maxbits > 32;
my $extra = defined $ENV{RELEASE_TESTING} && $ENV{RELEASE_TESTING};
@@ -34,7 +34,31 @@ push @testn, @testn64 if $use64;
push @testn, qw/9999986200004761 99999989237606677 999999866000004473/
if $use64 && $extra;
-plan tests => (2 * scalar @testn) + 1*$use64 + 6*7;
+my %all_factors = (
+1234567890 => [2,3,5,6,9,10,15,18,30,45,90,3607,3803,7214,7606,10821,11409,18035,19015,21642,22818,32463,34227,36070,38030,54105,57045,64926,68454,108210,114090,162315,171135,324630,342270,13717421,27434842,41152263,68587105,82304526,123456789,137174210,205761315,246913578,411522630,617283945],
+1032924637 => [6469,159673],
+4567890 => [2,3,5,6,10,15,30,43,86,129,215,258,430,645,1290,3541,7082,10623,17705,21246,35410,53115,106230,152263,304526,456789,761315,913578,1522630,2283945],
+ 456789 => [3,43,129,3541,10623,152263],
+ 123456 => [2,3,4,6,8,12,16,24,32,48,64,96,192,643,1286,1929,2572,3858,5144,7716,10288,15432,20576,30864,41152,61728],
+ 115553 => [],
+ 30107 => [7,11,17,23,77,119,161,187,253,391,1309,1771,2737,4301],
+ 42 => [2,3,6,7,14,21],
+ 16 => [2,4,8],
+ 12 => [2,3,4,6],
+ 10 => [2,5],
+ 9 => [3],
+ 8 => [2,4],
+ 7 => [],
+ 6 => [2,3],
+ 5 => [],
+ 4 => [2],
+ 3 => [],
+ 2 => [],
+ 1 => [],
+ 0 => [],
+);
+
+plan tests => (2 * scalar @testn) + 1*$use64 + scalar(keys %all_factors) + 6*7;
if ($use64) {
# Simple test: perl -e 'die if 18446744073709550592 == ~0'
@@ -66,6 +90,11 @@ foreach my $n (@testn) {
}
};
+while (my($n, $divisors) = each(%all_factors)) {
+ is_deeply( [all_factors($n)], $divisors, "all_factors($n)" );
+}
+
+
extra_factor_test("trial_factor", sub {Math::Prime::Util::trial_factor(shift)});
extra_factor_test("fermat_factor", sub {Math::Prime::Util::fermat_factor(shift)});
extra_factor_test("holf_factor", sub {Math::Prime::Util::holf_factor(shift)});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git
More information about the Pkg-perl-cvs-commits
mailing list