Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

renz's scratchpad

by renz (Scribe)
on Jan 14, 2005 at 21:55 UTC ( #422406=scratchpad: print w/ replies, xml ) Need Help??

#!/usr/bin/perl -w # prime.pl, v2.0 - test for primality # thanks to: mr_mischief for help and efficient isPrime() rewrite. # also: i prefer the code TedPride uses in node 430254 to find the # highest divisor, so i have modified my own here.. use strict; use warnings; @ARGV or die &usage; print "\n[RESULTS]\n\n"; foreach my $num (@ARGV) { isPrime($num); } sub isPrime { my $num = $_[0]; my $val = 'prime'; if ($num =~ /^\d+$/ && $num >= 2) { my $mod = 2; my $div = int sqrt $num; while ($mod <= $div) { ($num % $mod) == 0 ? ($val = 'composite', last) : $mod++; } } else { $val = 'neither'; } print ' ' . $num . ":\t\t$val\n"; } sub usage { print "\nUsage: " . $0 . " <number(s)>\n"; print "\n\tOnly real, whole numbers greater than one can be prime. +\n"; exit 1; } # rz/021005

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2015-07-29 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls