Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 rifling through the Monastery: (13)
As of 2014-07-22 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (130 votes), past polls