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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.