The I/O Complexity of Computing Prime Tables

  • Michael A. Bender
  • Rezaul Chowdhury
  • Alexander ConwayEmail author
  • Martín Farach-Colton
  • Pramod Ganapathi
  • Rob Johnson
  • Samuel McCauley
  • Bertrand Simon
  • Shikha Singh
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 9644)


We revisit classical sieves for computing primes and analyze their performance in the external-memory model. Most prior sieves are analyzed in the RAM model, where the focus is on minimizing both the total number of operations and the size of the working set. The hope is that if the working set fits in RAM, then the sieve will have good I/O performance, though such an outcome is by no means guaranteed by a small working-set size.

We analyze our algorithms directly in terms of I/Os and operations. In the external-memory model, permutation can be the most expensive aspect of sieving, in contrast to the RAM model, where permutations are trivial. We show how to implement classical sieves so that they have both good I/O performance and good RAM performance, even when the problem size N becomes huge—even superpolynomially larger than RAM. Towards this goal, we give two I/O-efficient priority queues that are optimized for the operations incurred by these sieves.


External-memory algorithms Prime tables Sorting Priority queues 



We thank Oleksii Starov for suggesting this problem to us.


  1. 1.
    Aggarwal, A., Vitter, S.: Jeffrey: the input/output complexity of sorting and related problems. Commun. ACM 31(9), 1116–1127 (1988)MathSciNetCrossRefGoogle Scholar
  2. 2.
    Agrawal, M., Kayal, N., Saxena, N.: Primes is in P. Ann. Math. 50, 781–793 (2004)MathSciNetCrossRefzbMATHGoogle Scholar
  3. 3.
    Arge, L.: The buffer tree: a technique for designing batched external data structures. Algorithmica 37(1), 1–24 (2003)MathSciNetCrossRefzbMATHGoogle Scholar
  4. 4.
    Arge, L., Bender, M.A., Demaine, E.D., Holland-Minkley, B., Munro, J.I.: Cache-oblivious priority queue and graph algorithm applications. In: Proceedings of the 34th Annual Symposium on Theory of Computing, pp. 268–276 (2002)Google Scholar
  5. 5.
    Arge, L., Thorup, M.: RAM-efficient external memory sorting. In: Cai, L., Cheng, S.-W., Lam, T.-W. (eds.) Algorithms and Computation. LNCS, vol. 8283, pp. 491–501. Springer, Heidelberg (2013)CrossRefGoogle Scholar
  6. 6.
    Atkin, A., Bernstein, D.: Prime sieves using binary quadratic forms. Math. Comput. 73(246), 1023–1030 (2004)MathSciNetCrossRefzbMATHGoogle Scholar
  7. 7.
    Bays, C., Hudson, R.H.: The segmented sieve of Eratosthenes and primes in arithmetic progressions to 1012. BIT Numer. Math. 17(2), 121–127 (1977)MathSciNetCrossRefzbMATHGoogle Scholar
  8. 8.
    Bengelloun, S.: An incremental primal sieve. Acta Informatica 23(2), 119–125 (1986)MathSciNetCrossRefzbMATHGoogle Scholar
  9. 9.
    Brent, R.P.: The first occurrence of large gaps between successive primes. Math. Comput. 27(124), 959–963 (1973)MathSciNetCrossRefzbMATHGoogle Scholar
  10. 10.
    Cox, D.A.: Primes of the Form \(x^2+ny^2\): Fermat, Class Field Theory, and Complex Multiplication. Wiley, New York (1989)Google Scholar
  11. 11.
    Dunten, B., Jones, J., Sorenson, J.: A space-efficient fast prime number sieve. IPL 59(2), 79–84 (1996)MathSciNetCrossRefzbMATHGoogle Scholar
  12. 12.
    Farach-Colton, M., Tsai, M.-T.: On the complexity of computing prime tables. In: Elbassioni, K., Makino, K. (eds.) ISAAC 2015. LNCS, vol. 9472, pp. 677–688. Springer, Heidelberg (2015). doi: 10.1007/978-3-662-48971-0_57 CrossRefGoogle Scholar
  13. 13.
    Gale, R., Pratt, V.: CGOL-an Algebraic Notation for MACLISP Users. MIT Artificial Intelligence Library, Cambridge (1977)Google Scholar
  14. 14.
    Galway, W.F.: Dissecting a sieve to cut its need for space. In: Bosma, W. (ed.) ANTS-IV. LNCS, vol. 1838, pp. 297–312. Springer, Heidelberg (2000)CrossRefGoogle Scholar
  15. 15.
    Gries, D., Misra, J.: A linear sieve algorithm for finding prime numbers. Commun. ACM 21(12), 999–1003 (1978)MathSciNetCrossRefzbMATHGoogle Scholar
  16. 16.
    Hardy, G.H., Wright, E.M.: An Introduction to the Theory of Numbers. Oxford University Press, Oxford (1979)zbMATHGoogle Scholar
  17. 17.
    Horsley, S.: KO\(\Sigma \)KINON EPATO\(\Sigma \Theta \)ENOY\(\Sigma \). or, the sieve of eratosthenes. being an account of his method of finding all the prime numbers, by the Rev. Samuel Horsley, FRS. Philos. Trans. 62, 327–347 (1772)CrossRefGoogle Scholar
  18. 18.
    Lenstra Jr., H.W.: Primality testing with gaussian periods. In: Agrawal, M., Seth, A.K. (eds.) FSTTCS 2002. LNCS, vol. 2556, pp. 1–1. Springer, Heidelberg (2002)CrossRefGoogle Scholar
  19. 19.
    Mairson, H.G.: Some new upper bounds on the generation of prime numbers. Commun. ACM 20(9), 664–669 (1977)MathSciNetCrossRefzbMATHGoogle Scholar
  20. 20.
    Mertens, F.: Ein beitrag zur analytischen zahlentheorie. J. fr die reine und angewandte Mathematik 78, 46–62 (1874)MathSciNetGoogle Scholar
  21. 21.
    News, F.: World’s largest prime number discovered - all 17 million digits, February 2013.,
  22. 22.
    Patrascu, M., Thorup, M., Dynamic integer sets with optimal rank, select, predecessor search. In: FOCS, pp. 166–175 (2014)Google Scholar
  23. 23.
    Pomerance, C., Selfridge, J.L., Wagstaff, S.S.: The pseudoprimes to \(25 \cdot 10^9\). Math. Comput. 35(151), 1003–1026 (1980)MathSciNetzbMATHGoogle Scholar
  24. 24.
    Pomerance, C., Shparlinski, I.E.: On pseudosquares and pseudopowers. Comb. Number Theor., 171–184 (2009)Google Scholar
  25. 25.
    Pritchard, P.: A sublinear additive sieve for finding prime number. Commun. ACM 24(1), 18–23 (1981)MathSciNetCrossRefzbMATHGoogle Scholar
  26. 26.
    Pritchard, P.: Explaining the wheel sieve. Acta Informatica 17(4), 477–485 (1982)MathSciNetCrossRefzbMATHGoogle Scholar
  27. 27.
    Pritchard, P.: Linear prime-number sieves: a family tree. Sci. Comput. Program. 9(1), 17–35 (1987)MathSciNetCrossRefzbMATHGoogle Scholar
  28. 28.
    Schönhage, A., Grotefeld, A., Vetter, E.: Fast algorithms: a multitape turing machine implementation. Wissenschaftsverlag, B.I (1994)Google Scholar
  29. 29.
    Singleton, R.C.: Algorithm 357: an efficient prime number generator. Commun. ACM 12, 563–564 (1969)CrossRefGoogle Scholar
  30. 30.
    Sorenson, J.: An introduction to prime number sieves. Technical report 909, Computer Sciences Department, University of Wisconsin-Madison (1990)Google Scholar
  31. 31.
    Sorenson, J.P.: The pseudosquares prime sieve. In: Hess, F., Pauli, S., Pohst, M. (eds.) ANTS 2006. LNCS, vol. 4076, pp. 193–207. Springer, Heidelberg (2006)CrossRefGoogle Scholar
  32. 32.
    Villarino, M.B.: Mertens’ proof of mertens’ theorem. arXiv:math/0504289 (2005)
  33. 33.
    Vitter, J.S.: External memory algorithms and data structures: dealing with massive data. ACM Comput. Surv. (CsUR) 33(2), 209–271 (2001)CrossRefGoogle Scholar
  34. 34.
    Williams, H.C.: Edouard Lucas and primality testing. Canadian Mathematics Society Series of Monographs and Advanced Texts, 22 (1998)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2016

Authors and Affiliations

  • Michael A. Bender
    • 1
  • Rezaul Chowdhury
    • 1
  • Alexander Conway
    • 2
    Email author
  • Martín Farach-Colton
    • 2
  • Pramod Ganapathi
    • 1
  • Rob Johnson
    • 1
  • Samuel McCauley
    • 1
  • Bertrand Simon
    • 3
  • Shikha Singh
    • 1
  1. 1.Stony Brook UniversityStony BrookUSA
  2. 2.Rutgers UniversityPiscatawayUSA
  3. 3.LIP, ENS de LyonLyonFrance

Personalised recommendations