What is the t h e n u m b e r of primes p less than 2016 such that both p + 4 and p + 1 2 are also prime ?
Hint:
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Use either sieve of eratosthenes or just a naive algo to check whether a number is prime or not. Then for each i that satisfies the condition, increment the answer and finally it will be 19.
How efficiently do these algorithms work to find if a number n is prime?
Log in to reply
If you use naive algo then to check whether a number is prime or not you have to check for n-2 numbers excluding 1 and n at worst case. So to check T cases of n's as prime or not, this algo took O(t*n) (I,e) if we need to check n numbers then overall complexity will be nearly O(n^2).
To reduce this complexity to nearest log factors, we are going with sieve of eratosthenes.
We know that if a number n is divisible by i where 1<i<n-1, then that n is composite and not prime. So just keep an array of size x where x is the largest 'n' that you are going to check whether prime or not. Initially make all the entries in it as 0(prime at first). Then carefully mark all the multiples of i where 1<i<n as 1(those are composite). We can furthur optimise this marking process just by leaving the already marked i's, because if i is already marked then all the multiples of that i should also be marked already. Now we are available with an array that says whether a particular index of that array is prime or not. This computation will take O(n (log n) loglog(n)) in the worst case and hence we reduced our complexity from O(n^2). This is how sieve efficiently works when we compare it with naive one.
Correct me if anything went wrong.
Log in to reply
Well.. I have not ever learned an algorithm for finding whether a number is prime or not. What I follow is a simple and efficient way below;
1 2 3 4 5 6 7 8 |
|
How does it work?
What do say about that ?
Log in to reply
@Zeeshan Ali – This one, will yields the worst case complexity of O(n^2) though you are considering only N/2 numbers. This one can also be furthur optimized. If you know what does that loglog(n) say, then you can easily get what sort of optimisation, I'm talking about. Google it to learn furthur optimising ur procedure. here it is: Consider the number 8. The divisor of 8 will be found between 1 and sqrt(8) itself. Because the largest number that divides 8 will be 4 and which in turn 4 will get 2 as the such number that divides it. So instead of running the loop untill N/2, you can change it to sqrt(N). This is evident that if 4 is a multiple of 2 then the number x which is the multiple of 4 is also the multiple of 2.
Log in to reply
@Vijay Kumar – Yes.. you are right.. I am trying to learn the better approaches. And that is why I gave the set Prime Crimes via Computer Science . Thank you very much for your contributions :)
Log in to reply
@Zeeshan Ali – All these prime crime problems are nice. Enjoyed solving it. Nice work dude keep it up!!! :-)
Log in to reply
@Vijay Kumar – I would like you to refresh your stats on Leader Board of this set :D
Log in to reply
@Zeeshan Ali – Leaderboard is not showing. Completed all except prime crime 2 in this set. What is there in leaderboard?I cant able to see.
Log in to reply
@Vijay Kumar – Well.. I don't know why the leader board is not showing.. And still there is nothing it that.. I expect those who attempt my problems on sets given by me in the list of their respective leader boards :)
Log in to reply
@Zeeshan Ali – I have realized that all my leader bords are empty due to some vague reasons. Please contact with Calvin Lin via calvin@Brilliant.org for that issue of "Leader Board is not opening!" for my set. Thanks
Problem Loading...
Note Loading...
Set Loading...
Here is a C++ code to find whether a number is prime or not.
Here is the complete code in c++ :)