rekcah,
First, I would like to say
Welcome to the Monastery and to Perl. Ok - everything everyone else has said is correct. There will be arguments on both sides on what I am about to say, but Perl was designed with deep roots in Unix - which originally didn't provide a mechanism to release memory back to the operating system. With this said, it is fairly adept at re-using memory without requesting more as long as the design of your program allows it to do its job. Future releases of Perl may even release memory back to the OS, but in the interim - lets fix your code. You will have to adapt it to your specific needs, but this should give you an idea.
#!/usr/bin/perl -w
use strict;
for my $test_number (5 .. 1000) {
next unless ($test_number % 2);
my ($prime , $factor) = IsPrime($test_number);
if ($prime) {
print "$test_number is prime\n";
}
else {
print "$test_number is not prime - divisible by $factor\n";
}
}
sub IsPrime {
my $input = shift;
my $t_number = 3;
while ($t_number < sqrt($input)) {
return (0, $t_number) unless ($input % $t_number);
$t_number += 2;
}
return 1;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.