Introduction
The Prime Spiral, also known as the Ulam Spiral, is a graphical representation of prime numbers arranged in a spiral pattern. In this pattern, numbers are arranged in a square spiral where prime numbers are marked, showing interesting patterns and symmetries.
In this tutorial, we will learn how to generate the Prime Spiral using the Java programming language.
Objective
The goal of this program is to generate a Prime Spiral where prime numbers are displayed in a spiral formation. The spiral starts from the center and spirals outwards, marking prime numbers. This can be an interesting and visually appealing way to display prime numbers.
Java Program Code
import java.awt.*;
import javax.swing.*;
public class PrimeSpiral extends JPanel {
private static final int SIZE = 500; // Panel size
private static final int GRID_SIZE = 101; // Grid dimensions (odd number)
// Method to check if a number is prime
public static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
// Method to draw the prime spiral
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, SIZE, SIZE);
g.setColor(Color.BLACK);
int centerX = SIZE / 2;
int centerY = SIZE / 2;
int step = 1; // Spiral movement step
int x = centerX, y = centerY;
int dx = 1, dy = 0; // Initial direction: right
// Loop through numbers and draw primes in the spiral pattern
for (int num = 1; num <= 10000; num++) {
if (isPrime(num)) {
g.fillOval(x - 3, y - 3, 6, 6); // Draw prime number as a small dot
}
// Move to next position in the spiral
x += dx * step;
y += dy * step;
// Change direction
if (dx == 1 && dy == 0) {
dx = 0;
dy = 1;
} else if (dx == 0 && dy == 1) {
dx = -1;
dy = 0;
} else if (dx == -1 && dy == 0) {
dx = 0;
dy = -1;
step++; // Increase step after each complete turn
} else if (dx == 0 && dy == -1) {
dx = 1;
dy = 0;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Prime Spiral (Ulam Spiral)");
PrimeSpiral spiral = new PrimeSpiral();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(SIZE, SIZE);
frame.add(spiral);
frame.setVisible(true);
}
}
Program Explanation
This program generates a prime spiral by creating a JPanel in Java and overriding the paintComponent()
method to draw the spiral on the screen. The isPrime()
method checks whether a number is prime. The program starts from the center of the panel and spirals outwards, drawing a dot whenever it encounters a prime number. The directions of the spiral change after each step, and the step size increases after each complete revolution.
How to Run the Program
1. Save the code in a file named PrimeSpiral.java.
2. Open your terminal or command prompt and navigate to the directory where the file is located.
3. Compile the program using the command:
javac PrimeSpiral.java
4. Run the compiled program with the command:
java PrimeSpiral
5. A window will open displaying the Prime Spiral, with prime numbers marked as small dots on the spiral grid.