Respected Monks,

This will perhaps go in the history of PerlMonks (and otherwise) as the stupidest prime number detector ever!!, but I am trying to learn how to create modules and needed something to work on. If after checking this script, the Monks here decide that I am too stupid to even attempt to create modules, I can certainly live with it.

So, as shown below, the script detects if the supplied number is a prime or not. If the number is a prime and is 5 or 6 digits, the script becomes somewhat slower. At 7 digit primes, it just starts getting exponentially slower and anything above that, it just hangs.

However, if the number is NOT a prime, and even if it is 20 digits, it shows the output in a flash!!

use strict; use warnings; use 5.10.1; use bignum; use Time::HiRes qw(gettimeofday tv_interval); print "Enter the integer: "; chomp (my $prime_candidate = <STDIN>) ; exit if $prime_candidate <= 0 && say "$prime_candidate is not a prime. +"; my $is_prime = 1; my $start_time = [gettimeofday]; foreach my $each_num (2..$prime_candidate) { if ($prime_candidate % $each_num == 0 && $prime_candidate - $each_ +num != 0 ) { say "$prime_candidate is not a prime"; $is_prime = 0; exit; } } my $elapsed_time = tv_interval($start_time); say "$prime_candidate is a prime " if $is_prime != 0; printf "The [$0] script took %2.2f seconds to finish\n", $elapsed_time +;

Given below are the outputs:

$ perl time_is_prime_v1.pl Enter the integer: 99929 99929 is a prime The [time_is_prime_v1.pl] script took 0.79 seconds to finish $ perl time_is_prime_v1.pl Enter the integer: 999983 999983 is a prime The [time_is_prime_v1.pl] script took 7.69 seconds to finish $ perl time_is_prime_v1.pl Enter the integer: 9999991 9999991 is a prime The [time_is_prime_v1.pl] script took 73.97 seconds to finish $ perl time_is_prime_v1.pl Enter the integer: 1320486952377516576 1320486952377516576 is not a prime

So why have I put up this monstrosity here? I just want to find a way to make it work faster so that I can create a module out of it by doing the required changes, test the output by checking it against the output of Math::Prime::Util and be happy that I've created a module!!. I will not ever dare to put it up on CPAN as I know it's too stupid :D, I just want to play with Perl.


In reply to 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.