Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Factors & Prime Factors: Limit input size to <= 1M (#4669)
Browse files Browse the repository at this point in the history
* Limit input size to Factors IA

* Limit input size of PrimeFactors Goodie
  • Loading branch information
moollaza committed Nov 28, 2018
1 parent 9ca1876 commit 28d2459
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion lib/DDG/Goodie/Factors.pm
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions lib/DDG/Goodie/PrimeFactors.pm
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions t/Factors.t
Expand Up @@ -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,
);
Expand Down
9 changes: 5 additions & 4 deletions t/PrimeFactors.t
Expand Up @@ -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',
Expand All @@ -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;

0 comments on commit 28d2459

Please sign in to comment.