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 ( [id://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
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-28 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found