XP is just a number | |
PerlMonks |
Re: a close prime numberby renz (Scribe) |
on Feb 11, 2005 at 22:45 UTC ( [id://430337]=note: print w/replies, xml ) | 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.
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.
In Section
Seekers of Perl Wisdom
|
|