"be consistent" PerlMonks

### Re: Prime Number Finder

by The_Rev (Acolyte)
 on Feb 20, 2002 at 14:13 UTC ( #146582=note: print w/replies, xml ) Need Help??

in reply to Prime Number Finder

I had made the post above ragrading half the iterations, but I just thought of a better way to calculate primes. Its not as fast, but its a one liner: while(\$num <= \$max) { push @dynamic, \$num if (1 x \$num) !~ /^(11+)\1+\$/; \$num ++; } I hope you find this helpful

Replies are listed 'Best First'.
Re: Re: Prime Number Finder
by blakem (Monsignor) on Feb 20, 2002 at 18:30 UTC
"but I just thought of a better way to calculate primes"
Did you actually "just think that up" cause it looks quite a bit like one that abigail wrote:
```perl -wle 'print "Prime" if (1 x shift) !~ /^1?\$|^(11+?)\1+\$/' [number
+]

-Blake

How does this work?
The regular expression match tries to break the string "111.." (a string of 1's as long as your input number) into some number of equal-length pieces of length two or more. It uses non-greedy matching, so it starts with the smallest possible and works up.. "11","111","1111" and so on. This is the same order of efficiency as the OP's prime test loop (one at a time), but it uses the regular expression engine.
my apologies to Blake, no I didn't 'just think that up', it actually is abigails code with a slight modication.
Re: Re: Prime Number Finder
by I0 (Priest) on Feb 20, 2002 at 23:31 UTC
1 is not prime
Whether or not 1 is prime is a question of definitions.

While I admit that it makes more sense to me to say that 1 is not a prime, there is certainly not universal agreement on it. In particular (as I discovered when I took some advanced number theory courses) a number of the people who undertook to compute long lists of primes started their lists with 1. After a while you learn not to be too dogmatic about it. (Though I have to say that there is far more agreement that 1 is not prime than there is on, say, whether 0 is a natural number.)

"Whether or not 1 is prime is a question of definitions."
Indeed. It is not prime by definition.

This is a useful definition since it allows integers to have a unique prime factorization.

(Less controversially, 0 is not prime, although it is also listed by the program which prompted this comment)

Create A New User
Node Status?
node history
Node Type: note [id://146582]
help
Chatterbox?
 Discipulus is going mad with the new win10 client.. tried mv Cortana /dev/null but no succes [Corion]: Ouch - another pretrained set is available, using images from IMDB and Wikipedia. So I expect interesting results for non-PR images where people are pictured that are not made up [Corion]: Discipulus: Yeah, from my investigations, you can somewhat silence+disable Cortana, but some services of it remain always running unfortunately. What a waste of resources :-/ [Corion]: I hope you had a good weekend still ;) choroba had a workshop with the band [choroba]: which counts as a good weekend [Discipulus]: yes, (at least until Sun afternoon...): Saturday we got splendid birthday party in a park: lot of eat, drink and children amusement: bag running, magnetic fishing, rope and that big pot full of candies to smash with a club

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-09-25 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (277 votes). Check out past polls.

Notices?