Skip to content

The Ulam Spiral is a graphical representation of prime numbers in a spiral pattern. This program generates the Ulam Spiral and displays it using Java's Swing library.

Notifications You must be signed in to change notification settings

okdotdev/ulam-spiral-java-swing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ulam Spiral

This program generates an Ulam Spiral, a visualization of prime numbers in a spiral pattern. It uses Java Swing to create a graphical user interface (GUI) window for displaying the spiral. 🌀

Usage

  1. Run the program. ▶️
  2. A dialog box will prompt you to enter the size of the spiral. 🔢
  3. Enter a positive integer value to determine the size of the spiral. The larger the size, the more prime numbers will be displayed.
  4. The program will open a window titled "Ulam Spiral" displaying the generated spiral. 🪐
  5. Each prime number in the spiral is represented by a small square filled in the graphical interface. 🟩

Prerequisites

  • Java Development Kit (JDK) installed ☕
  • Java Swing library

How it works

  1. The program prompts the user to enter the size of the spiral using a dialog box.
  2. The Main class extends the JFrame class to create a window for the spiral.
  3. The window is centered on the screen and sized according to the user's input.
  4. The paint method is overridden to draw the spiral.
  5. The prime numbers are determined using the isPrime method, which checks if a number is divisible by any number from 2 up to the square root of the number.
  6. The Graphics object is used to draw the prime numbers as small squares on the spiral.
  7. The spiral is generated by moving in a clockwise direction, changing direction at each "turn" and incrementing the step size.
  8. The dx and dy variables control the movement of the spiral in the x and y directions, respectively.
  9. The angle variable keeps track of the current angle of movement in degrees.
  10. The loop continues until all the cells in the spiral are processed.
Ulam Spiral

Note

  • If the user enters an invalid input or closes the dialog box without entering a value, a default size of 100 is used for the spiral.

Feel free to customize and experiment with the program to explore different sizes and variations of the Ulam Spiral. ✨

Links

Don't forget to check out the GitHub repository for the source code and the YouTube video for a visual demonstration of the program! 🚀

About

The Ulam Spiral is a graphical representation of prime numbers in a spiral pattern. This program generates the Ulam Spiral and displays it using Java's Swing library.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages