Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2017-04-30 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (534 votes). Check out past polls.