Java

 

 

 

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.

 

© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)