Chapter 15. Prime Numbers

Prime numbers have fascinated professional and amateur mathematicians alike since ancient times. It's a simple concept—a prime number is any integer greater than 1 that is divisible only by 1 and itself. An integer greater than 1 that is not prime is composite. In this chapter, we'll use Java to explore two topics related to prime numbers.

The first topic is primality testing. Given a positive integer, how can we tell whether it is prime or composite? Of course, we can systematically try dividing the number by a succession of “trial divisors”—if we find a divisor other than 1 and the number itself that divides evenly into the number (the division leaves no remainder), then the number is composite. If we fail to find such ...

Get Java™ Number Cruncher: The Java Programmer's Guide to Numerical Computing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.