Problem 1763. Primes for Large N: 2^30, System Memory Limit
This Challenge is to further improve the "primes" function for speed/memory usage to the Limit of Cody Memory.
The Matlab function "primes" has a very efficient sieving method but it suffers from memory usage issues, especially on limited memory systems.
The Primes Faster Challenge gives the first half to maximizing the primes function for optimal memory usage and speed.
Cody appears to have 2GB of RAM based upon "out of memory" messages observed.
The test case of 2^30 pushes the upper limits of Cody memory.
The reference solution can process N=2^32 on an 8GB machine in 68sec.
Input: N (max of primes to find)
Output: vector of primes (all primes less than or equal to N)
Scoring: Time to find all primes < 2^30
Hints:
1) Doubles use 8 bytes; 2) The primes method p(((k*k+1)/2):k:q) creates a vector using q/k*8 bytes. This is 8/3 the size of p for k=3 because k is a double. 3) Segmentation will keep memory usage below limits at the cost of time. 4) Cody Available Memory appears to be 537.27MB. XP Operating system eats memory. 5) Usage of profiler and Task Manager combined give performance insights.
Solution Stats
Problem Comments
-
3 Comments
Nice problem!
I have had some problems to get trough the test suite, one of them was that my primes where in UINT32 and that failed the 'isprime' assertion.
Maybe you could change the following line
pchk=p(ptr);
to
pchk=double(p(ptr));
Double implemented in test suite. Intent is to find the primes, not to get snared by errant function limitations.
Currently the MATLAB function primes is able to solve this problem. Since systems always evolve, the problem's author will probably have to be constantly updating it. Or at least, once a year.
Solution Comments
Show commentsProblem Recent Solvers25
Suggested Problems
-
345 Solvers
-
1365 Solvers
-
Change the sign of even index entries of the reversed vector
553 Solvers
-
388 Solvers
-
453 Solvers
More from this Author308
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!