Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Simple primality testing

by spiritway (Vicar)
on Nov 23, 2005 at 05:49 UTC ( [id://511005]=note: print w/replies, xml ) Need Help??


in reply to Simple primality testing

You might consider generating a list of primes below a given number, and keeping it on disk. Then randomly access one of those numbers. I don't know exactly how many primes are less than 10**8 or so, but it's not altogether overwhelming.

Replies are listed 'Best First'.
Re^2: Simple primality testing
by ambrus (Abbot) on Nov 23, 2005 at 09:47 UTC
Re^2: Simple primality testing
by Anonymous Monk on Nov 23, 2005 at 16:56 UTC
    #!/usr/bin/perl -w # a not too bad approximation to the number of primes # less than N is approximately N/log(N). Better methods # exist, but none are as simple... my $n1 = 10**8; my $n2 = 2**32; $\="\n"; # ~ 5428681 print "Number of primes less than $n1 is ",int($n1/log($n1)); # ~ 193635250 print "Number of primes less than $n2 is ",int($n2/log($n2));

      Actually since these are signed longs it only goes up to 2^31-1, which is a prime, a Mersenne Prime actually. (For those that missed this bit, a Mersenne prime is any prime expressable in the form 2^N-1.)

      I dont know if I'm the only one that finds it interesting that the highest number you can represent with a long is a prime, but I do. :-)

      Now if only they would use questions like this on pub-quizzes. :-)

      ---
      $world=~s/war/peace/g

        Pub quizzes would probably prefer 'prime' in reference to beef rather than integers.

        --hsm

        "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-26 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found