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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.