The Ulam spiral is a graphical depiction of the set of prime numbers, devised by mathematician Stanislaw Ulam. It is constructed by writing the positive integers in a square spiral and specially marking the prime numbers.You can read more about it here.
But we are going to compute on the alternate version of this spiral where the prime numbers are lined up in the spiral rather than natural numbers as in the original Ulam’s spiral.
The prime numbers are written in a spiral form starting from the origin (0, 0) and moving as shown in the diagram above. The numbers shown in the right column and the bottom row are the column numbers and row numbers respectively ( i.e. y and x coordinates)
The objective is to find the position (x and y coordinates) of a given prime number.
Input : 5 Output : 1 1 As per the diagram above 5 corresponds to the column 1 and row 1. Input : 11 Output : -1 1 Similarly, 11 will correspond to column -1 and row 1.
Approach: The general solution to the problem would be to create an algorithm to predict the position of the moving spiral point at every step and reference each step in the spiral to a prime number (e.g. step 0 –> 2; step 1 –> 3 ; step 2 –> 5 and so on). Using this prime number we can trace back to the co-ordinates which is our solution (2 –> [0, 0], 3 –> [1, 0], 5 –> [1, 1]).
We begin by keeping count of x and y, for step k = 1 : x = y = 0. k = 2 leads to increase in x by 1 (x = 1) next comes k = 3 providing y = 1. Now as the spiral rotates we are to decrease the values now by 2. The consecutive movements are shown below:
From the above computations, we observe that the operations are same for a batch of steps, e.g. at first K = 2 & 3 follow one where x is increased by 1 and then y by 1. Next in K= 4, 5, 6, 7 for 4 and 5, x is decreased and y is decreased in K = 6 and 7. Again From K = 8, 9 and 10, x is increased in repeated steps and 11, 12, 13 consists of decreasing y.
Each step can be classified into a batch and the size of batch increases by 1 after two runs. After a run, the operation is switched from y to x or vice versa and after every two batch runs the addition operation is switched to subtraction and vice versa. The following table explains it.
Lastly, we also need to create a prime number generator that will keep on feeding the above algorithm with a prime number that will be referenced to a corresponding step and it will give us the required coordinates.
Here is the implementation of above idea :
Input Limits : 2 < N < 1000000
Assumption : Prime numbers are the only inputs to the program.
This article is contributed by Pritom Gogoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Quick ways to check for Prime and find next Prime in Java
- Print the nearest prime number formed by adding prime numbers to N
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Check if a prime number can be expressed as sum of two Prime Numbers
- Given a number x , find y such that x*y + 1 is not a prime
- Program to find the next prime number
- Program to find the Nth Prime Number
- Find prime number K in an array such that (A[i] % K) is maximum
- Find largest prime factor of a number
- Find sum of a number and its maximum prime factor
- Find all the prime numbers of given number of digits
- Find a sequence of N prime numbers whose sum is a composite number
- Analysis of Different Methods to find Prime Number in Python
- Find the number of primitive roots modulo prime
- Find Largest Special Prime which is less than or equal to a given number