O'Reilly logo

Programming with Java by Sunil Patekar, Mahesh Bhave

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 14

COLLECTION OF ACADEMIC PROGRAMS

14.1 Prime Numbers

Prime numbers are very important in mathematics as well as in computer science. There are many algorithms for finding prime numbers. Let us write programs based on two common algorithms.

14.1.1 Sieve of Eratosthenes

Sieve of Eratosthenes algorithm is taught in high school mathematics. This is a simple algorithm. It is not necessarily the most efficient. The idea behind it is very simple. Cross out (remove) all numbers which are multiples of any prime numbers. Naturally, all those numbers which remain intact are prime numbers.

The working procedure can be explained as follows.

First, draw a 10 by 10 grid and write numbers 1 to 100 in it (Figure 14.1). Next draw a square around 1, as ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required