A few days back i was watching a video on YouTube uploaded by Numberphile (video below) and i was quite fascinated by it. Fascination led to implementation and the thought of implementation led to code. Oh! I must admit that that the thought of looking cool once i achieved this did cross my mind :). Below is the code and a screenshot of the plot of prime numbers as seen in the video. The spiral is called the Ulam Spiral after the mathematician Stanislaw Ulam. You can find the Wikipedia article here. Don’t forget to read up on how Ulam came up with this spiral.

Screenshot

Ulam Spiral

A plot of Ulam Spiral/Prime Spiral

UlamSpiral.java


import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;

/**
 * Created by Tanmay Dharmaraj on 26-12-2014.
 */

class BoxSpiral extends JPanel{
    int height,width,diameter=3, plotDistance=3;
    double max_prime;

    public BoxSpiral(int ulam_height,int ulam_width,double ulam_max_prime) {
        height = ulam_height;
        width = ulam_width;
        max_prime = ulam_max_prime;
    }

    private void plot(String s,int x,int y,Graphics2D g2d){
        //g2d.drawString(s,xCoor,yCoor);
        g2d.fillOval(((height/2)+x)+(x*plotDistance),((width/2)+y)+(y*plotDistance),diameter,diameter);
    }

    private boolean isPrime(int number) {
        for (int i = 2; i < Math.sqrt(number); i++) {
            if (number % i == 0 && i != number) return false;
        }
        return true;
    }

    private void drawUlam(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        int counter = 1;
        int inc=20;
        int directionCounter =0; //0=Right - Up; 1=Left - Down;
        int start_x=0,start_y=0;
        int primeNumber=1;
		g.setColor(Color.red);
        plot(Integer.toString(primeNumber),start_x,start_y,g2d);
        primeNumber++;
		g.setColor(Color.black);
		while(primeNumber <= max_prime){
            if(directionCounter==0){
                //Right
                for(int c=0;c<counter;c++) {
                    if(isPrime(primeNumber))
						plot(Integer.toString(primeNumber), ++start_x, start_y, g2d);
					else
						++start_x;
					primeNumber++;
                }
                //Up
                for(int c=0;c<counter;c++) {
                    if(isPrime(primeNumber))
						plot(Integer.toString(primeNumber),start_x,--start_y,g2d);
					else
						--start_y;
					primeNumber++;
                }

                directionCounter=1;
                counter++;
            }
            else{
                //Left
                for(int c=0;c<counter;c++) {
                    if(isPrime(primeNumber))
						plot(Integer.toString(primeNumber),--start_x,start_y,g2d);
					else
						--start_x;
					primeNumber++;
                }
                //Down
                for(int c=0;c<counter;c++) {
                    if(isPrime(primeNumber))
						plot(Integer.toString(primeNumber),start_x,++start_y,g2d);
					else
						++start_y;
					primeNumber++;
                }
                directionCounter=0;
                counter++;
            }
        }
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        drawUlam(g);
    }
}

public class UlamSpiral extends JFrame {
    int height,width;
    public UlamSpiral(int ulam_height,int ulam_width){
        height=ulam_height;
        width =ulam_width;
        initialize();
    }

    private void initialize() {
       setTitle("Ulam Spiral - Box Pattern");
        add(new BoxSpiral(height,width,10000));
        setSize(height,width);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                UlamSpiral us = new UlamSpiral(750,750);
                us.setVisible(true);
            }
        });
    }
}