Per Anomalous Monk (and the original intention of the post) "Ok, you've got the lousy algorithm. So where's the module and its .t script? That seems to me to be the thing to focus on right now."
So here goes. Here's what I have:
I changed the directory to prime_scripts cause that's where I have everything related to this stuff
$ cd prime_scripts/
The tree command shows the contents of the directory.
$ tree
.
├── lib
│ └── LousyPrime.pm
├── README.md
└── t
└── prime_test_1.t
2 directories, 3 files
Here's the output of the very basic test. Note:- The v flag was purposefully not included in the prove output below so as to avoid long running output.
$ prove -l t/prime_test_1.t t/prime_test_1.t .. ok All tests successful. Files=1, Tests=1000, 0 wallclock secs ( 0.06 usr 0.01 sys + 0.14 cu +sr 0.01 csys = 0.22 CPU) Result: PASS $
Here's the Perl Module created based on the script. The output had to be tweaked to match that of Math::Prime::Util is_prime() function.
package LousyPrime;
use strict;
use warnings;
use Exporter qw(import);
our $VERSION = '0.001';
$VERSION = eval {$VERSION};
our @EXPORT_OK = qw (is_lousy_prime);
sub is_lousy_prime {
my $prime_candidate = shift @_;
if ( $prime_candidate <= 0 ) {
return 0;
exit;
}
exit if $prime_candidate == 1 && return 0;
my $bool_is_prime = 1;
foreach my $each_num (2..sqrt $prime_candidate) {
if ($prime_candidate % $each_num == 0 && $prime_candidate - $each_num != 0 ) {
$bool_is_prime = 0;
return 0;
exit;
}
}
return 2 if $bool_is_prime != 0;
}
1;
And here is the test script
use strict; use warnings; use 5.10.1; use Math::Prime::Util qw (is_prime); use Test::More; use FindBin::libs; use LousyPrime qw(is_lousy_prime); for (1..1000) { say is_prime($_), is_lousy_prime($_); is ( is_prime($_) , is_lousy_prime($_), "matches"); } done_testing;
There's a lot more other ways to test, but this is what I've come up with as of now. If the revered monks here do find anything I can improve in the test code written above, please do let me know.
In reply to Re: Stupidest Prime Number detector ever!!
by Anonymous Monk
in thread Stupidest Prime Number detector ever!!
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |