Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
The stupid question is the question not asked
 
PerlMonks  

Re^2: a close prime number

by Anonymous Monk
on Feb 11, 2005 at 20:46 UTC ( #430263=note: print w/ replies, xml ) Need Help??


in reply to Re: a close prime number
in thread a close prime number

I am thinking of using Math::Prime::Simple. The way with that was:

@ranges = ( [ $myNum-100, $myNum+100 ] ); $primes = prime( @ranges );

But this gives me ranges from -100 to +100 of $myNum. And I would have to parse through and check to see the 2 primenumbers smaller and greater than $myNum. Is there any nicer way?

Thanks


Comment on Re^2: a close prime number
Download Code
Re^3: a close prime number
by dragonchild (Archbishop) on Feb 11, 2005 at 20:50 UTC
    There is no way of predicting what the next prime number is, given any other number. If there was, then current cryptography methods wouldn't work. All prime number generators are, for the most part, variations on brute force. The method you're looking at is probably as good as any.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Okay thanks. BTW the quotes at the end of your posts are wise and correct in my opinion.
      This node was taken out by the NodeReaper on Thu Feb 17 17:19:12 2005 (EST)
      Reason: [Revelation]: A clone of node 430391.

      For more information on this node visit: this

        Please remove this node, I posted it as an Anonymous Monk erroenously.
      There is no way of predicting what the next prime number is, given any other number. If there was, then current cryptography methods wouldn't work. All prime number generators are, for the most part, variations on brute force. The method you're looking at is probably as good as any.

      Huh?!? You're joking, aren't you? The same fact that you say "all prime number generators are, for the most part, variations on brute force" is in contradiction with the claim that "there is no way of predicting what the next prime number is, given any other number".

      More precisely if I have any isprime() sub, e.g.

      sub isprime { my $n=shift; return undef if abs $n == 1; return isprime -$n if $n<0; return 0 if $n == 0; $n%$_ or return 0 for (2..sqrt $n); 1; }
      (I'm not posting a better one for others already have, and even better ones are available elsewhere.) or the witty
      sub isprime { (1 x shift) !~ /^1?$|^(11+?)\1+$/; }
      (this is Abigail's - who else?) then a sub like this:
      sub nearest_prime { my $n=my $m=shift; return $n if isprime $n; while (1) { return $n if isprime ++$n; return $m if isprime --$m; } }
      would answer the OP's question.

      Important: not only primality testing algorithms do exist, but fast ones exist too. What in some sense there is "no way" to do is to factorize a composite number which is a different problem. But really even for the latter problem there are perfectly deterministic algorithms. "Only" it is believed to be computationally "hard" (in a mathematically precise sense), whereas the former is not. It is on this last conjecture (not proved yet!) that many cryptography methods rely.

      Since the OP did not specify the size of a problem, but gave an example with small numbers, an effective answer in terms of some efficient sieving algorithm can be given.

        Let me rephrase: There is no mathematical way to predict what the next prime number is, given any other number. In other words, there is no f(x) that will return the prime number closest to x without looking at either x-1 or x+1 first. (Unless, of course, either of those is a prime number.)

        Furthermore, every single prime number generator is a variation on brute force. Your own nearest_prime() is a perfect example of this - you look at every number above and below n until you find one that is prime. That is a brute force algorithm.

        As for primality tests, I refer you to Google's cache of the Wikipedia article (as it seems that en.wikipedia.com currently isn't available). Specifically, there are two quotes by some rather famous mathematicians. I'll repeat the one by Euler:

        Mathematicians have tried in vain to this day to discover some order in the sequence of prime numbers, and we have reason to believe that it is a mystery into which the human mind will never penetrate.

        If there is no order in the sequence of the primes, then there is no way to predict the closest prime to a given number. It is possible to determine it, but not to predict it.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re^3: a close prime number
by sleepingsquirrel (Hermit) on Feb 11, 2005 at 21:06 UTC
    You can do better than using the arbitrary range of +100/-100, (which isn't going to be good enough when your numbers get larger). Here's a an introductory explaination of prime number distribution you might find helpful.


    -- All code is 100% tested and functional unless otherwise noted.
      You can do better than using the arbitrary range of +100/-100, (which isn't going to be good enough when your numbers get larger).
      Really? Is there a range of 200 consecutive numbers within which there are no primes? My understanding was that they appeared with regular density, no matter how high you climbed.

      Update:
      And I get to answer my own question. I Googled up this webpage. It's 210 numbers to the next prime from 20831323.


      Caution: Contents may have been coded under pressure.
        Really? Is there a range of 200 consecutive numbers within which there are no primes? My understanding was that they appeared with regular density, no matter how high you climbed.
        Huh?!? You're joking too, arent' you? Can you find many prime numbers amongst the numbers q+2, q+3, ..., q+p, where q is the product of all primes up to p? This is so elementary that it is IIRC on the second or third page of "Hardy and Wright"...
        My understanding was that they appeared with regular density
        Regular density? Depending on what you mean by "regular" .. There is the famous prime number theorem. If π(n) is the number of primes less than n, then π(n) tends to n/ln(n) as n grows to infinity. Thus you could say that the density if primes within {1,..,n} is ln(n)/n. But this density gets smaller and smaller as n gets larger, thus the primes have to be getting (on average) farther and farther apart.

        Also, as blazar mentions below. If you want to find a number X such that the first prime after X is at least X+200, just let X be 201! (that's with a factorial). Now X+1 may be prime, but X+2 is not prime since 2 divides X and 2. X+3 is not prime since 3 divides X and 3, etc... all the way until X+201.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-04-19 21:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls