Factors & Prime Factors: Limit input size to <= 1M (#4669)

* Limit input size to Factors IA

* Limit input size of PrimeFactors Goodie
master
Zaahir Moolla 2018-11-28 13:02:26 -05:00 committed by GitHub
parent 9ca1876212
commit 28d2459d4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 5 deletions

View File

@ -22,7 +22,12 @@ handle remainder => sub {
# query number is negative, find and include negative factors.
my $negative = $1;
my $query_mag = $2;
my @factors = divisors($query_mag);
# max input value of 1M
# anything big takes too long to calculate
return unless $query_mag <= 1000000;
my @factors = divisors($query_mag);
unshift @factors, sort { $a <=> $b } map { -$_ } @factors
if $negative;

View File

@ -102,6 +102,8 @@ handle remainder => sub {
# Extract only the number from the remainder
$_ =~ s/\D+//;
return unless $_ <= 1000000;
my @factors = factor_exp($_);
# Exit if we didn't find anything.

View File

@ -56,6 +56,7 @@ ddg_goodie_test(
'-12',
'-12, -6, -4, -3, -2, -1, 1, 2, 3, 4, 6, 12'
),
'factors of 999999999' => undef,
'factors of 2.4' => undef,
'factors of fear' => undef,
);

View File

@ -27,9 +27,9 @@ sub build_answer {
}
ddg_goodie_test(
[qw(
DDG::Goodie::PrimeFactors
)],
[qw(
DDG::Goodie::PrimeFactors
)],
'72 prime factors' => test_zci('The prime factorization of 72 is 2^3 × 3^2',
build_answer('72 - Prime Factors', '2³ × 3²')),
'prime factors of 111' => test_zci('The prime factorization of 111 is 3 × 37',
@ -52,7 +52,8 @@ ddg_goodie_test(
build_answer(undef, '83 is a prime number')),
'is 83 a prime number' => test_zci('83 is a prime number',
build_answer(undef, '83 is a prime number')),
'optimus prime 45' => undef
'optimus prime 45' => undef,
'prime factors of 9999999' => undef
);
done_testing;