Prime number less than or equal to N

  • Thread starter Thread starter Avichal
  • Start date Start date
  • Tags Tags
    Prime
Click For Summary

Homework Help Overview

The discussion revolves around finding the largest prime number less than or equal to a given number N, where N can be as large as 10^18. Participants are exploring methods to efficiently determine primality and identify the largest prime within this range.

Discussion Character

  • Exploratory, Mathematical reasoning, Assumption checking

Approaches and Questions Raised

  • Some participants suggest a brute-force method of counting down from N to find a prime, while others question the feasibility of this approach due to the size of N. There are mentions of researching primality tests and using computational tools like Mathematica and Maxima for efficiency.

Discussion Status

Participants have shared various insights and tools that can assist in finding primes, including specific functions in Mathematica and Maxima. There is an acknowledgment of the limitations of certain methods and the potential for faster algorithms, such as the Miller-Rabin test, which has been noted for its effectiveness with large numbers.

Contextual Notes

There is a recognition of the computational challenges posed by large values of N, and some participants express concern over the time required for traditional primality tests. The discussion includes references to the prime number theorem and the distribution of primes, which may influence the strategies considered.

Avichal
Messages
294
Reaction score
0

Homework Statement


Given a number N<=1018, I need to find the largest prime number less than or equal to N


Homework Equations





The Attempt at a Solution


I can only think of a brute-force solution i.e. iterate from N in decreasing order until you get a prime number.
And to check if number is prime just iterate till its square-root. If it has no factors then it is prime else its not.

But this won't work as Nmax = 1018 which will take too much time.
Any ideas?
 
Physics news on Phys.org
Avichal said:

Homework Statement


Given a number N<=1018, I need to find the largest prime number less than or equal to N


Homework Equations





The Attempt at a Solution


I can only think of a brute-force solution i.e. iterate from N in decreasing order until you get a prime number.
And to check if number is prime just iterate till its square-root. If it has no factors then it is prime else its not.

But this won't work as Nmax = 1018 which will take too much time.
Any ideas?

You can start by googling about it. Like "prime number tests". Get one and then start counting backwards. That's one way. The lazy way is to use Mathematica's PrimeQ. Better in my opinion to first give it a few hours, maybe one then of just researching on the internet how to check a number for prime, maybe write some code to implement the process even if you do it only for much smaller numbers. Get some idea about what to do then if your efforts fail for 10^(18), find a machine running Mathematica and code it.
 
jackmell said:
You can start by googling about it. Like "prime number tests". Get one and then start counting backwards. That's one way. The lazy way is to use Mathematica's PrimeQ. Better in my opinion to first give it a few hours, maybe one then of just researching on the internet how to check a number for prime, maybe write some code to implement the process even if you do it only for much smaller numbers. Get some idea about what to do then if your efforts fail for 10^(18), find a machine running Mathematica and code it.

I'd agree with that. Testing all of the numbers up to sqrt(n) is a pretty time intensive primality test, it will take hours. On the other hand the prime number theorem tells you that about one in every log(10^18)~41 numbers is prime in that neighborhood. So you don't have to test that many numbers. Mathematica is not your only other option if you want a canned primality test. Get a copy of 'maxima'. It's free! It has a 'primep' function. The first prime below 10^18 is 999999999999999989. Took a second or two once you've written the function to count backwards.
 
I would have thought Wolfram Alpha could do this, but it seems to choke on numbers as large as 1000000000000000000

But, Mathematica itself has a function NextPrime[10^18,-1] that finds the prime in question in 16 milliseconds. :-)
 
The Electrician said:
I would have thought Wolfram Alpha could do this, but it seems to choke on numbers as large as 1000000000000000000

But, Mathematica itself has a function NextPrime[10^18,-1] that finds the prime in question in 16 milliseconds. :-)

Ok, checking the actual timing maxima finds the prime in about 2ms and will factor a number of that size in about 400ms (worst case, where the number is actually prime). Reading the description of the algorithm it uses a Miller-Rabin pseudo-primality test for numbers over 10^16, so there is a vanishing small probability it might be wrong. Using 'factor' shows it's not. Oh, and I also wrote a naive version of the sqrt(n) test and applied it to that number a few hours ago. I'd tell you how long it took, but it's still running.
 
Last edited:
Dick said:
Ok, checking the actual timing maxima finds the prime in about 2ms and will factor a number of that size in about 400ms (worst case, where the number is actually prime). Reading the description of the algorithm it uses a Miller-Rabin pseudo-primality test for numbers over 10^16, so there is a vanishing small probability it might be wrong. Using 'factor' shows it's not. Oh, and I also wrote a naive version of the sqrt(n) test and applied it to that number a few hours ago. I'd tell you how long it took, but it's still running.

Miller-Rabin pseudo-primality test works like a charm. Its very fast for numbers as high as 1018.
Thanks a lot!
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
5K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
9
Views
2K