# Java program to find coordinates of a prime number in a Ulam Spiral

In this tutorial, we will learn how to find the coordinates of a prime number in the program. We would be using Ulam Spiral method to find the desired output. We are using this method so as to get the prime numbers using Java programming.

## Finding Coordinates of a prime number using Ulam Spring in Java

The Ulam spiral is also called as Prime Spiral. This method is constructed by writing the he positive integers in a square spiral and specially marking the prime numbers. The square spiral starts with the number one at the center but can start with any number. And the primes at its vertical, diagonal and horizontal.

In this program, we first created a class named prach and then created a method named sp. In this method, we passed the input as of the integer type so that it could take the numbers as the input. We used the method as static and void so that the numbers could be in the same range.

public class prach{ static void sp(int input) { int step_count = 1; int step_limit = 2; int adder = 1; int a = 0, b = 0;

In this part, we have applied various loops like for, if and also if-else. This keeps the track of the coordinates. The for loop keeps the track of each step. If the number entered is prime then the number and the step_count is one. So the step limit is increased and likewise, the step limit is also increased. At the last print statement, the numbers are stored.

for (int n = 2; n != input + 1; n++, step_count++) { if (step_count <= .5 * step_limit) a += adder; else if (step_count <= step_limit) b += adder; if (step_count == step_limit) { adder *= -1; step_limit += 2; step_count = 0; } } System.out.print( a + " " + b); }

The numbers that we get as the a and b these is then replaced with the step k. If we provide the input to a number then if the number is prime then only the further instructions will be executed. And we get the prime_c equals to some number is stored.

static int prime(int input) { int p, cnt, prime_c = 0; for (int i = 2; i <= 1000000; i++) { cnt = 0; for (p = 2; p <= i; p++) { if (i % p == 0) cnt++; } if (cnt == 1) { prime_c++; if (input == i) { input = prime_c; break; } } } return input; }

This is the main part of the program from where the execution starts. We provide the input as the number 131 and call both the functions that we have created earlier. After that, we could see the result of our program.

public static void main(String args[]) { int input = 131; sp(prime(input)); }

OUTPUT:

The Output is: 3,2

PROGRAM:

public class prach{ static void sp(int input) { int step_count = 1; int step_limit = 2; int adder = 1; int a = 0, b = 0; for (int n = 2; n != input + 1; n++, step_count++) { if (step_count <= .5 * step_limit) a += adder; else if (step_count <= step_limit) b += adder; if (step_count == step_limit) { adder *= -1; step_limit += 2; step_count = 0; } } System.out.print( a + " " + b); } static int prime(int input) { int p, cnt, prime_c = 0; for (int i = 2; i <= 1000000; i++) { cnt = 0; for (p = 2; p <= i; p++) { if (i % p == 0) cnt++; } if (cnt == 1) { prime_c++; if (input == i) { input = prime_c; break; } } } return input; } public static void main(String args[]) { int input = 131; sp(prime(input)); } }

## Leave a Reply