Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Prime Number Finder

by gu (Beadle)
on Nov 11, 2005 at 12:47 UTC ( #507703=note: print w/ replies, xml ) Need Help??


in reply to Prime Number Finder

There's an interesting algorithm for finding whether a number is prime in the documentation of Quantum::Superpositions, which provides quantum-like superpositions in Perl :

use Quantum::Superpositions ; sub is_prime { my ($n) = @_; return $n % all(2..sqrt($n)+1) != 0 }
See the documentation for details on how quantum-like superpositions work.

Gu

Update : Removed assertion on the complexity of this algorithm


Comment on Re: Prime Number Finder
Download Code
Re^2: Prime Number Finder
by blokhead (Monsignor) on Nov 11, 2005 at 13:46 UTC
    Nice succinct algorithm, but I must take issue with
    There's an interesting O(1) algorithm
    You do have to execute the algorithm on a classical computer, so Q::S or not, it's most definitely not O(1). It'll be exponential (in the number of bits in $n) because behind the scenes, Q::S is dividing $n by all possible factors (what else could it be doing?). But even on a quantum computer, you still need either a division or gcd circuit (and probably a lot of other stuff), which will take some polynomial time in the number of bits.

    Just because it's a one-liner doesn't make it O(1). Anyway, my favorite cutesy inefficient primality checker is

    sub is_prime { ("1" x $_[0]) !~ /^(11+)\1+$/ }

    blokhead

      some polynomial time in the number of bits.
      If the number of bits is constant, then any polynomial time based on it is constant.

      Caution: Contents may have been coded under pressure.
        If the number of bits is constant, then any polynomial time based on it is constant.
        Big-O statements (like an algorithm taking constant or O(1) time) are statements about asymptotic behavior, i.e, how the function behaves in the limit (usually, as input size tends to infinity). If you don't look at them in the limit, then big-O-ish language (like constant time) is meaningless.

        How meaningless? Even undecidable languages have a constant time "algorithm" if you consider the input size to be held to a constant. So without viewing things in the limit, all problems become computationally equivalent in the asymptotic language.

        Update: added citation from parent node

        blokhead

Re^2: Prime Number Finder
by hv (Parson) on Nov 11, 2005 at 14:59 UTC

    Even if you had a quantum computer to run it on, I'm not convinced you'd have an O(1) algorithm unless you also have O(1) algorithms for computing sqrt($n) and 2..$n.

    In any case however the algorithm will give the wrong answer for 2. You can fix that by replacing sqrt($n)+1 with sqrt($n+1).

    Hugo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://507703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-12-28 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls