R
Robin
check out some of my perl programs,
http://offlame.memebot.com
let me know if you like them....
Thanks
-robin
http://offlame.memebot.com
let me know if you like them....
Thanks
-robin
R> check out some of my perl programs,
R> http://offlame.memebot.com
you have a lot of perl and programming to learn still. [...]
$_ = "$q" / "$m";
that is horrible in so many ways. why did you put "" around the
variables? especially since you then do a math operation on them. why
did you assign the result to $_ instead of a named variable?
if ($_ !~ /\./) {
did you know you could use named variables with the !~ operator? and
checking if a number has a fractional part with a regex is very
clunky. it is better to stick to pure math ops like int(), * and /.
you have a lot of perl and programming to learn still. PJH> [...]
$_ = "$q" / "$m";
that is horrible in so many ways. why did you put "" around the
variables? especially since you then do a math operation on them. why
did you assign the result to $_ instead of a named variable?
if ($_ !~ /\./) {
did you know you could use named variables with the !~ operator? and
checking if a number has a fractional part with a regex is very
clunky. it is better to stick to pure math ops like int(), * and /.
No, the OP needs to stop making some silly mistakesUri said:R> check out some of my perl programs,
R> http://offlame.memebot.com
you have a lot of perl and programming to learn still.
Ralph Malph said:Uri Guttman wrote:
Oh, and if you were bothered by Uri's comments
then please do not worry. He is a morbidly obese
shut in. One of the aforementioned jaded losers.
Nobody has seen him in months...his
doorways are now far to narrow to allow him out.
Even if he turns sideways.
I must hand it to him though, it is quite a novel
way to fight foreclosure!
Robin said:
Why didn't you do thisJens said:I unfortunately have to agree with others here that didn't
like your code - but I only looked at the program for gene-
rating prime numbers and the "text editor" yet. And both are
not winners... Here's the one for generating prime numbers:
1 #!/usr/bin/perl
2 use strict;
3 my $npflag = 0;
4 my $n;
5 my @z;
6 print "Prime number generator starting at 3 and going up....\n";
7
8 foreach my $q (4 .. 10000) {
9 $npflag = 0;
10 foreach my $m (1 .. $q) {
11 next if $m == 1;
12 next if $m == $q;
13 $_ = "$q" / "$m";
14 if ($_ !~ /\./) {
15 $npflag = 1;
16 }
17 }
18
19 if (! $npflag) {
20 print "We have found prime : $q\n";
21 sleep 1;
22 }
23 }
Lets forget about the unused variables and that kind of stuff.
a) What you print out in line 6 isn't correct, you start with 4,
and not with 3 as you claim.
b) Including even numbers in checks for primes is a strange idea,
they are simple to avoid and are never prime (except 2;-)
c) Starting with $m set to 1 in line 10 and then checking in
line 11 that $m is not 1 is very strange, to say the least.
d) The same holds for going up to $q and then checking that $m
isn't $q.
e) Going up to $q instead of it's square root shows that you
should do a bit more research on prime numbers before you
atart writing programs to find primes.
f) The double quotes in line 13 indicate that you don't well
understand really what they are used for in Perl.
g) Using '$_' as a variable looks to me as bad as you can get.
h) Using a regex to check if a number can be divided by another
one is, to say the least, strange ("inefficent" doesn't cover
it appropriately).
And then, even if you remove the call to sleep(), it's slow as
molasses. Take the following program, that does logically more
or less the same as yours (using the same rather uneducated al-
gorithm):
#!/usr/bin/perl
use strict;
use warnings;
print "Prime number generator starting at 3 and going up....\n";
NOT_A_PRIME:
for ( my $q = 3; $q <= 10000; $q += 2 ) {
for ( my $m = 3; $m <= sqrt( $q ); $m += 2 ) {
next NOT_A_PRIME unless $q % $m;
}
print "We have found prime : $q\n";
}
[snip]Why didn't you do thisJens said:Robin said:
I unfortunately have to agree with others here that didn't
like your code - but I only looked at the program for gene-
rating prime numbers and the "text editor" yet. And both are
not winners... Here's the one for generating prime numbers:
[snip]
Lets forget about the unused variables and that kind of stuff.
[snip]
And then, even if you remove the call to sleep(), it's slow as
molasses. Take the following program, that does logically more
or less the same as yours (using the same rather uneducated al-
gorithm):
#!/usr/bin/perl
use strict;
use warnings;
print "Prime number generator starting at 3 and going up....\n";
NOT_A_PRIME:
for ( my $q = 3; $q <= 10000; $q += 2 ) {
for ( my $m = 3; $m <= sqrt( $q ); $m += 2 ) {
next NOT_A_PRIME unless $q % $m;
}
print "We have found prime : $q\n";
}
for ( my $q = 3; $q <= 10000; $q += 2 ) {
my $limit = sqrt($q);
for ( my $m = 3; $m <= $limit; $m += 2 ) {
next NOT_A_PRIME unless $q % $m;
}
and you save 52187 unnecessary calls to sqrt in
this example alone!
ha! nice catch.my ($q, $m,$limit);
for ( $q = 3; $q <= 10000; $q += 2 ) {
$limit = sqrt($q);
for ( $m = 3; $m <= $limit; $m += 2 ) {
next NOT_A_PRIME unless $q % $m;
}
and 52187 + 5000 unnecessary initializations.
Ralph Malph said:No, the OP needs to stop making some silly mistakes
but he is clearly a quite capable programmer.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.