Let ζ ( n ) be the largest prime strictly less than n . Evaluate the sum n = 2 ∑ 2 5 0 0 ζ ( h n )
Details and assumptions
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.
another general algorithm is to first generate all primes less than h n which can be done quickly using a sieve. Then you simply start a pointer at the first prime number, namely 2, and for each h n you move the pointer forward until you reach the largest prime less than h n at which point you increment the running sum by that prime number. Once you've stepped through all the values for n the running sum will contain the answer.
I use a raw approach to evaluate ζ ( n ) but it works since n < 2 5 0 1
Here is my code -
1 2 3 4 5 6 7 8 9 |
|
Note that each time you call ζ ( n ) , you recompute all the number of prime numbers up to n . Is there a way to avoid all this re-calculation?
Yup, my code is similar to yours. Here 's my code (C++). I think I should start learning Python. It takes comparatively less code to do some work in Python than it takes in C++, it seems.
By the way, I later realized (and edited my code accordingly) that you don't need to use square root in the loop condition (although this might not be a problem in python since you don't need to include any external library to use square root). For those using C/C++, they can just edit the loop condition to
i*i<=n
to avoid using
sqrt()
and hence make a code without using any external libraries.
Log in to reply
That's true. Python provides a lot of good inbuilt features which simplifies the code and makes it much shorter.
In response to challenge master: Actually I'm not recomputing all the prime numbers upto n when I call ζ ( n ) . Instead I am reversing the search. I start by checking if n − 1 is prime. Then I check if n − 2 is prime and so on.
Here's my take on the problem:So basically I used the Miller-Rabin primality test,the code is available here
1 2 3 4 5 6 7 8 9 |
|
My method took about 5 minutes, but that's because my primality test was fairly slow. I calculated h = 2 n 2 − n , then ran a For Loop that counted down from h by 1 until it reached a prime number, and then I added the number to the running total.
My primality test involved using a function I had written previously that gave a list of all the divisors of the number, and if that list had a length of 2 (1 and itself) then it concluded it was prime. I feel that with a faster primality test this method could be fairly quick.
Try Miller Rabin.
Problem Loading...
Note Loading...
Set Loading...
prime[i]
to store the largest prime smaller than i .prime[k]
for all k = h i for i = 2 … 2 5 0 0 .An alternative approach is to use a fast primality test algorithm called Miller-Kabin test . The code in the wiki is quite inefficient. Below is an improved version mainly on faster computation of a d m o d n .
On my machine, both algorithm takes roughly the same runtime (4~5 sec). However, as n increases, I believe the first approach will be less efficient. In addition, the second approach use much less memory than the first one. Miller-Kabin for the win!