### Comment on

 Need Help??

Update: Removed entire program and restructured post around isPrime() -- full code here.

Update2: For those of you who aren't familiar with the problems of Fermat's Little Theorem check out this.

This isn't *exactly* what you wanted, but since mr_mischief and I were talking about it yesterday, and a primality question popped up today, I thought I'd post it. If nothing else, you can make use of isPrime()... It could be easily adapted to check a range of numbers for primes, a task in this case left to the reader.

```# thanks to: mr_mischief for help and efficient isPrime() rewrite.

sub isPrime {
my \$num = \$_[0];
my \$val = 'prime';
if (\$num =~ /^\d+\$/ && \$num >= 2) {
my \$mod = 2;
my \$div = int(\$num / 2);
while (\$mod <= \$div) {
(\$num % \$mod) == 0 ? (\$val = 'composite', last) : \$mod++;
}
} else { \$val = 'neither'; }
print ' ' . \$num . ":\t\t\$val\n";
}

# rz/021005

A couple of nice things about this isPrime() is that it should not be fooled by pseudoprimes (because we aren't exactly using Fermat's little theorem to test for them) or any data that should not be prime (thanks to /^\d+\$/). Without that regex, weird things would happen (well, weird in the context of what we are doing), like returning prime for numbers that are obviously not prime:

`(3.5 % 2) = 1`
(which evaluates to prime -- best to ignore decimals and negatives since they can't be prime anyway.)

/renz.
"Are you merely the spirit of these/ Bones that shelter me?"
--Dax Riggs, Dressed Up In Smoke.

In reply to Re: a close prime number by renz
in thread a close prime number 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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [Eily]: such a fine and clever workaround though... [LanX]: well even my random fingers have memory ... [Eily]: you could have tried randomly swapping your fingers [Eily]: enigma style [choroba]: so it was you who typed the whole phrases?

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2017-03-28 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (331 votes). Check out past polls.