Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I hope this is a good place to put this. I was on www.ca-osi.com doing some geek challenges and started perusing the site for some ideas how to solve one of the puzzles when I happened upon a prime number generator. I wondered about how the developer was doing his generator and the fact that it only went from 1 to 100. I decided to have my own go at it. Well, here it is. If you folks could take a gander at it and tell me what you think I'd appreciate it. If you guys like it I will place it in snippets. I don't want to place something there that is buggy or cludgy which is why I figured placing it here first would be a good idea.

#!/usr/bin/perl -w use strict; use File::Basename; my $plProgName = basename($0); my $plBaseName = fileparse($plProgName,'\.[^\.]*'); my $input = $ARGV[0] || &usage; my $fail = 0; if ( $input =~ /[a-z]/i ) { &usage(2); } print "Big numbers will take a little while...\n\n" if ( $input > 100000 ); for ( 1 .. $input ) { my $up = $_; my $last_digit = substr($up,length($up) - 1,1); my $mod = $input % $up; next if ( $last_digit == 5 or $last_digit = 0 ); if ( $mod == 0 ) { $fail++; # print "\$fail: $fail\n"; unless ( $input == 1 or $input == 2 or $input == 5 ) { if ( $input == $up and $fail == 2 ) { print "$input is a prime\n"; exit(0); } } else { print "$input is a prime\n"; exit(0); } } } print "$input is \c[[1mnot\c[[m prime.\n"; sub usage { my $errcode = shift || 1; print "\nUsage: $plProgName integer\n\n"; exit($errcode); }
Oh, now this isn't a generator. This is a number checker. So just run it with a number to check. I am not doing any bounds checking so if you use a huge number this script will be limited by what your machine arch can handle. I am also not checking for negative numbers so Im not sure how it will work if you use one.

Any and all feedback would be very appreciated.

I made some small changes to the other guy's code (since his was a generator and mine was a checker...I made his a checker and left his algorythm alone) and tried checking how quick his worked compared to mine and mine was significantly faster than his. This is a learning project for me so I am not looking at getting ripped to shreds unless it will be beneficial to me. :) TIA guys.

_ _ _ _ _ _ _ _ _ _
- Jim
Insert clever comment here...


In reply to YAPNC: Yet another prime number checker? by snafu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
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 goofing around in the Monastery: (3)
As of 2024-03-29 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found