Discovering prime numbers, those fascinating natural numbers greater than 1 that have only two positive divisors: 1 and themselves, is an intriguing pursuit in mathematics. Programming this concept in Java without relying on pre-built functions allows you to delve deep into the fundamentals of number theory and coding. Let's embark on this journey to understand prime numbers and how to identify them programmatically in Java.

Before we dive into the code, let's briefly recap what we mean by prime numbers. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The first six prime numbers are 2, 3, 5, 7, 11, and 13. Notably, 2 is the only even prime number.

Understanding the Prime Number Calculation Logic
To identify prime numbers programmatically, we need to understand the basic logic behind their calculation. A number is considered prime if none of the numbers before it can divide it without a remainder. The largest number that can possibly divide a given number is always the number itself, so we only need to check for divisibility up to the square root of the given number.

In Java, you can implement this logic within a simple for loop. The loop iteratively checks each number from 2 to the square root of the given number, and if the number is divisible by any of these, it is not a prime number.
Naive Approach: Checking All Numbers

The simplest approach to check if a number is prime is to iterate from 2 to this number, and if any number divides it without a remainder, it is not prime. Here's a simple Java function implementing this:
boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
However, this approach has a time complexity of O(n), which is inefficient for large numbers. A more optimized approach is to only check divisibility up to the square root of the number, as mentioned earlier.

Optimized Approach: Checking Up To the Square Root
To optimize the prime-checking function, we can reduce the upper limit of our loop to the square root of the given number. This way, we significantly reduce the number of iterations, making the function more efficient. Here's the optimized Java function:
boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}

Now that we have discussed the logic behind prime number identification, let's see how we can use these functions to list prime numbers within a given range.
Listing Prime Numbers within a Given Range



















Once you have a function to check if a number is prime, you can use it to list all prime numbers within a given range. Here's a simple Java function that lists all prime numbers from 2 to a given number:
void listPrimes(int n) {
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
This function uses the previously discussed isPrime function to check each number within the given range. If the number is prime, it is printed to the console.
Listing Primes in a Range using Sieve of Eratosthenes
A more efficient way to list all prime numbers up to a given number is to use the Sieve of Eratosthenes algorithm. This ancient algorithm uses the principle that any number greater than 1 is divisible by a prime number less than or equal to its square root. Here's a Java function implementing this algorithm:
void sieveOfEratosthenes(int n) {
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++) {
if (prime[i])
System.out.print(i + " ");
}
}
The Sieve of Eratosthenes algorithm first initializes all numbers as prime. It then iteratively marks the multiples of each prime number starting from 2, until the square of the current prime number is greater than the given number. Finally, it lists all remaining numbers as primes.
Primarily, programming prime numbers in Java without functions gives you a solid understanding of number theory and algorithm design. The efficiency and elegance of these algorithms, such as the Sieve of Eratosthenes, demonstrate the beauty of mathematics and computer science. With every number you check, you're not only identifying a prime, but you're also learning and growing as a programmer.