babug_prg has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: To Findout Prime number
by andreas1234567 (Vicar) on Feb 08, 2008 at 08:30 UTC | |
There's an interesting animation here that shows how the algorithm works. See also Math::Prime::XS.
-- Andreas | [reply] [d/l] [select] |
by lodin (Hermit) on Feb 08, 2008 at 13:45 UTC | |
It includes clever use of the vec operator. Maybe there's historical reasons for using vec over substr, but nowadays it's much faster to use substr. The for my $foo ($from .. $to) { ... } construct is also slightly more efficiently than for (my $foo = $from; $foo <= $to; $foo++) { ... }. My guess is that the C-style loop was used as the "range loop" wasn't optimized back then and actually generated a list of, in this case, a million numbers. I changed the for loop and did a direct translation to using substr instead of vec, and here's the results: Here's the code:
lodin | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Feb 08, 2008 at 15:20 UTC | |
The main advamtage of vec over substr is going 8 x higher for any given amount of ram. Of course, you can go twice as high and a little quicker, with either, by not wasting space for even numbers. And once you start saving space, you can go as far as 8 bits for every 30 numbers and higher still. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
by andreas1234567 (Vicar) on Feb 11, 2008 at 06:57 UTC | |
nowadays it's much faster to use substrI re-ran your benchmark on Linux 2.6.9 i686, perl v5.8.5 built for i386-linux-thread-multi. The difference was much less than what you encountered. On Linux 2.6.22-10-386 i686, perl v5.10.0 (different hardware than above): Update Mon Feb 11 12:13:38 CET 2008: Added perl 5.10 benchmark on lodin's request.
-- Andreas | [reply] [d/l] [select] |
|
Re: To Findout Prime number
by poolpi (Hermit) on Feb 08, 2008 at 07:38 UTC | |
See Abigail one-liners here ;)Thanks Abigail ! PooLpi
'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
| [reply] [d/l] |
by ZlR (Chaplain) on Jul 17, 2009 at 12:31 UTC | |
perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/' OK so I thought i'd elaborate a little on how this works since the monks from the chatterbox took time to explain it to me :)
So you have a number, say 6, and "write" it as a sequence of 1 ie (111111) .
ELISHEVA explains the math behind this : It means that if you can write the number as M groups of K ones, then it factorizes as : M * [ Sum(p=1->k) 1 ] which really is just M * K Which means that our number can be divided by M (or K), and therefore it's not prime. Shmem gives an example: i.e. for m = 1763, the group found would be 11111111111111111111111111111111111111111 repeated 43 times - not prime So how does the regexp implement that ? We can decompose it like this : So the real trick is in (11+?). In a standalone context, it will only match 11. That's because +? means "once or more but the minimum number of times". On the other hand, ^(11+?)$ will match a whole sequence of ones from begining to end.
Here (11+?) is followed by \1+$ which means : itself, once or more, until the end of the line . I understand that's what backtracking is. Eventually the regexp engine will try every grouping of ones and fine none. The only problem is that for some numbers you get a Complex regular subexpression recursion limit (32766) exceeded error. My guess was that it happens when the engine has to try more than 32766 number of times, ie the first fail appears for a number that needs K>32766. That's not the case though, since prime number 32779 does not yield the error. 65558 does, though. I went on and brutforced it, to find that the error appears first with 65536, which is 2 * 32768, which computes since it needs two groups to match... | [reply] [d/l] [select] |
|
Re: To Findout Prime number
by hipowls (Curate) on Feb 08, 2008 at 07:19 UTC | |
It is difficult to comment without knowing what the function is supposed to do. As far as I can tell it prints out all the numbers less than 1,000 that are not divisible by 2, 3, 5 or 7. Now just for fun
| [reply] [d/l] |
|
Re: To Findout Prime number
by GrandFather (Saint) on Feb 08, 2008 at 07:15 UTC | |
And the question is? Perl is environmentally friendly - it saves trees | [reply] |