Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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?
[ambrus]: Corion: actually, schmorp has complained about how you used that syntax in the draft for AnyEvent::Impl:: Prima. I don't think he screamed, but whatever.
[ambrus]: Yes, I know you fixed that since.

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2017-02-27 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (388 votes). Check out past polls.