How many primes p less than 1 0 5 exist such that:
p m o d 1 0 4 , p m o d 1 0 3 , p m o d 1 0 2 and p m o d 1 0 are also prime?
As an explicit example, 2017 is one of them since 2017,17 and 7 are all prime.
Hint: The answer is a perfect square.
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.
Yeah, that's the trial division approach that you used. Good thinking for using a DP approach to make it more efficient. But even this will become slow with increase in input (range till you need to test)
If you modify your code to take the range as input from user and input 1 0 6 , you'll see the relative difference between the efficiency of the algorithms.
Both give the correct answer of 4 1 8 4 when you input 1 0 6 but your code takes ~9 secs and my code takes ~1.5 secs. See the difference?
Now, speaking of those algorithms, there are many primality testing algorithms, each with their own advantages and disadvantages. For the problem here, the best approach is to store a list of primes below the range and then test which of those primes satisfy the problem criteria, hence using a sieving approach works best. The sieve I used is called the Sieve of Eratosthenes . There are many other sieves but this one's the easiest to implement.
There are also other pseudoprimality (probabilistic) testing algorithms like Fermat primality test, Miller Rabin primality test, etc and deterministic tests like Lucas-Lehmer, etc. You can read about some of them in the Primality testing wiki .
Log in to reply
Exactly, I know that it is going to eat up too much of runtime for bigger inputs, that is why I wanted to know what algorithm you used. I have heard of the Sieve of Eratosthenes but never implemented it. Nice solution. It takes O(n) space complexity, right? It will show memory overflow for huge input but I think it will work fine till 10^8.
Log in to reply
Yup, the classic sieve of Eratosthenes has a space complexity of O ( n ) . Without deteriorating the runtime, we can reduce the space complexity to O ( n ) using the segmented sieve of Eratosthenes but reducing the space complexity furthermore will cause runtime to deteriorate.
If space complexity is even more of an issue, you can use the Sieve of Sorenson which has a worse runtime but better space complexity.
Log in to reply
@Prasun Biswas – Yes, I read about it in the Wiki link up there. :) Thanks a lot. I was looking for a code on primality testing for a long time besides the one I do.
Here is a C++ code to find whether a number is prime or not.
1 2 3 4 5 6 7 8 9 10 |
|
Here is the complete code in c++ :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Output:
625
What was the run-time of your code with that
is_prime()
function?
Log in to reply
Well.. in worst case.. it runs 2 n − 1 times. But the problem is with the numbers that are too long greater than 1 0 5 . Hence the function given by me is easy to understand but not efficient.
Log in to reply
I'm not asking about theoretical time complexity of your code. I'm asking about the actual average run-time (in seconds) of your code.
Log in to reply
@Prasun Biswas – Well.. the time you are asking may differ due to different hardware architectures. Our focus is the Efficiency of a code which in this case is O ( 2 n )
Log in to reply
@Zeeshan Ali – I know that. I assume you have a PC with an average configuration like mine, so I wanted a rough estimate of how much time it took for you to highlight how much inefficient the trial division approach is (without delving much into time complexity and stuff)
Simple sieving and using a hash-table (dictionary in python, I think) to store the sieving results and use for condition checking should yield much better results.
Log in to reply
@Prasun Biswas – How much time does yours take ?
Log in to reply
@Zeeshan Ali – Roughly 0.3 seconds. Here's the code (Python 3.5) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Output:
Enter range = 100000
Answer is 625
Approximate Runtime = 0.31 seconds
Problem Loading...
Note Loading...
Set Loading...
Here is my code, http://ideone.com/ruFRSR
@Prasun Biswas Please tell me about the primality testing algorithm you were talking about. I used the simplest one that I know, one that check all integers to sqrt(n).