Find coordinates of a prime number in a Prime Spiral - GeeksforGeeks

16 Mar, 2022
Introduction 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.Examples:  
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.  
C++
Java
Python
C#
PHP
Javascript
// C++ Program to find coordinates of a// prime number in a Prime Spiral#include <bits/stdc++.h> using namespace std; // The main algorithm that keeps track of// coordinatesvoid spiralSplicer(int input){    // Batch size tracker    int step_count = 1;     // Batch size limiter    int step_limit = 2;     // switches between -1 and +1    int adder = 1;     // Co-ordinates of step K    int x = 0, y = 0;     for (int n = 2; n != input + 1; n++,                       step_count++) {         // Keeps track of steps        // Checks on the current batch        if (step_count <= .5 * step_limit)            x += adder; // Switch to operating on x         else if (step_count <= step_limit)            y += adder; // Switch to operating on x                 if (step_count == step_limit) {              // Changes adder to -1 and vice versa            adder *= -1;            // Keeps on updating 'step_limit'           step_limit += 2;                       // Resets count           step_count = 0;        }    }    cout << x << " " << y;} int primeIndex(int input){    int j, cnt, prime_c = 0;    for (int i = 2; i <= 1000000; i++) {        cnt = 0;        for (j = 2; j <= i; j++) {            if (i % j == 0)                cnt++;        }         if (cnt == 1) {            prime_c++;             if (input == i) {            /* Replaces the prime number with              Step K which will be fed into              the main algorithm*/                input = prime_c;                break;            }        }    }    return input;} // driver codeint main(){    int input = 113;     // Prime Index Finder Output ported    // to final algorithm    spiralSplicer(primeIndex(input));}
// java Program to find coordinates of a// prime number in a Prime Spiralpublic class GFG {         // The main algorithm that keeps track of    // coordinates    static void spiralSplicer(int input)    {                 // Batch size tracker        int step_count = 1;             // Batch size limiter        int step_limit = 2;             // switches between -1 and +1        int adder = 1;             // Co-ordinates of step K        int x = 0, y = 0;             for (int n = 2; n != input + 1;                    n++, step_count++)        {                 // Keeps track of steps            // Checks on the current batch            if (step_count <= .5 * step_limit)                             // Switch to operating on x                x += adder;                 else if (step_count <= step_limit)                             // Switch to operating on x                y += adder;                         if (step_count == step_limit)            {                     // Changes adder to -1 and                // vice versa                adder *= -1;                     // Keeps on updating 'step_limit'                step_limit += 2;                                     // Resets count                step_count = 0;            }        }                 System.out.print( x + " " + y);    }         static int primeIndex(int input)    {        int j, cnt, prime_c = 0;        for (int i = 2; i <= 1000000; i++)        {            cnt = 0;            for (j = 2; j <= i; j++)            {                if (i % j == 0)                    cnt++;            }                 if (cnt == 1)            {                prime_c++;                     if (input == i)                {                         /* Replaces the prime                    number with Step K which                    will be fed into                    the main algorithm*/                    input = prime_c;                    break;                }            }        }                 return input;    }             // Driver code    public static void main(String args[]) {         int input = 113;         // Prime Index Finder Output ported        // to final algorithm        spiralSplicer(primeIndex(input));     }} // This code is contributed by Sam007.
# Python Program to find coordinates of a# prime number in a Prime Spiral # The main algorithm that keeps track of# coordinatesdef spiralSplicer(inp):        # Batch size tracker    step_count = 1      # Batch size limiter    step_limit = 2      # switches between -1 and +1    adder = 1      # Co-ordinates of step K    x, y = 0, 0      for n in range(2, inp + 1):            # Keeps track of steps        # Checks on the current batch        if (step_count <= .5 * step_limit):            x += adder # Switch to operating on x          else if (step_count <= step_limit):            y += adder # Switch to operating on x                  if (step_count == step_limit):            # Changes adder to -1 and vice versa            adder *= -1              # Keeps on updating 'step_limit'            step_limit += 2                         # Resets count            step_count = 0        step_count += 1    print x, y  def primeIndex(inp):    cnt, prime_c = 0, 0    for i in range(2, 1000000 + 1):        cnt = 0        for j in range(2, i + 1):            if (i % j == 0):                cnt += 1                 if (cnt == 1):            prime_c += 1              if (inp == i):                  """ Replaces the prime number with                    Step K which will be fed into                    the main algorithm """                inp = prime_c                break    return inp  # driver codeinp = 113 # Prime Index Finder Output ported# to final algorithmtemp = primeIndex(inp) spiralSplicer(temp) #This code is contributed by Sachin Bisht
// C# Program to find coordinates of a// prime number in a Prime Spiralusing System; class GFG {         // The main algorithm that keeps track of    // coordinates    static void spiralSplicer(int input)    {                 // Batch size tracker        int step_count = 1;             // Batch size limiter        int step_limit = 2;             // switches between -1 and +1        int adder = 1;             // Co-ordinates of step K        int x = 0, y = 0;             for (int n = 2; n != input + 1;                     n++, step_count++)        {                 // Keeps track of steps            // Checks on the current batch            if (step_count <= .5 * step_limit)                             // Switch to operating on x                x += adder;                 else if (step_count <= step_limit)                             // Switch to operating on x                y += adder;                         if (step_count == step_limit)            {                     // Changes adder to -1 and                // vice versa                adder *= -1;                     // Keeps on updating 'step_limit'                step_limit += 2;                                     // Resets count                step_count = 0;            }        }                 Console.Write( x + " " + y);    }         static int primeIndex(int input)    {        int j, cnt, prime_c = 0;        for (int i = 2; i <= 1000000; i++)        {            cnt = 0;            for (j = 2; j <= i; j++)            {                if (i % j == 0)                    cnt++;            }                 if (cnt == 1)            {                prime_c++;                     if (input == i)                {                         /* Replaces the prime                    number with Step K which                    will be fed into                    the main algorithm*/                    input = prime_c;                    break;                }            }        }                 return input;    }             // Driver code    public static void Main ()    {        int input = 113;         // Prime Index Finder Output ported        // to final algorithm        spiralSplicer(primeIndex(input));    }       } // This code is contributed by Sam007.
<?php// PHP Program to find coordinates of a// prime number in a Prime Spiral // The main algorithm that// keeps track of// coordinatesfunction spiralSplicer($input){         // Batch size tracker    $step_count = 1;     // Batch size limiter    $step_limit = 2;     // switches between -1 and +1    $adder = 1;     // Co-ordinates of step K    $x = 0; $y = 0;     for ($n = 2; $n != $input + 1; $n++,                           $step_count++)    {         // Keeps track of steps        // Checks on the current batch        if ($step_count <= .5 * $step_limit)                     // Switch to operating on x            $x += $adder;         else if ($step_count <= $step_limit)                     // Switch to operating on x            $y += $adder;                 if ($step_count == $step_limit)        {             // Changes adder to -1            // and vice versa            $adder *= -1;         // Keeps on updating        // 'step_limit'        $step_limit += 2;                     // Resets count        $step_count = 0;        }    }    echo $x, " ", $y;} function primeIndex($input){    $j;    $cnt;    $prime_c = 0;    for ($i = 2; $i <= 1000000; $i++)    {        $cnt = 0;        for ($j = 2; $j <= $i; $j++)        {            if ($i % $j == 0)                $cnt++;        }         if ($cnt == 1)        {            $prime_c++;             if ($input == $i)            {                 // Replaces the prime                // number with Step K                // which will be fed into                // the main algorithm                    $input = $prime_c;                    break;            }        }    }    return $input;}     // Driver Code    $input = 113;     // Prime Index Finder Output ported    // to final algorithm    spiralSplicer(primeIndex($input));     // This Code is contributed by ajit?>
<script>    // Javascript Program to find coordinates of a    // prime number in a Prime Spiral         // The main algorithm that keeps track of    // coordinates    function spiralSplicer(input)    {                   // Batch size tracker        let step_count = 1;               // Batch size limiter        let step_limit = 2;               // switches between -1 and +1        let adder = 1;               // Co-ordinates of step K        let x = 0, y = 0;               for (let n = 2; n != input + 1; n++, step_count++)        {                   // Keeps track of steps            // Checks on the current batch            if (step_count <= .5 * step_limit)                               // Switch to operating on x                x += adder;                   else if (step_count <= step_limit)                               // Switch to operating on x                y += adder;                           if (step_count == step_limit)            {                       // Changes adder to -1 and                // vice versa                adder *= -1;                       // Keeps on updating 'step_limit'                step_limit += 2;                                       // Resets count                step_count = 0;            }        }                   document.write( x + " " + y);    }           function primeIndex(input)    {        let j, cnt, prime_c = 0;        for (let i = 2; i <= 1000000; i++)        {            cnt = 0;            for (j = 2; j <= i; j++)            {                if (i % j == 0)                    cnt++;            }                   if (cnt == 1)            {                prime_c++;                       if (input == i)                {                           /* Replaces the prime                    number with Step K which                    will be fed into                    the main algorithm*/                    input = prime_c;                    break;                }            }        }                   return input;    }         let input = 113;       // Prime Index Finder Output ported    // to final algorithm    spiralSplicer(primeIndex(input)); </script>
Output :  
3 2
This article is contributed by Pritom Gogoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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. 
Sam007
jit_t
mukesh07
simmytarika5
Prime Number
spiral
Mathematical
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Program for Decimal to Binary Conversion
Find all factors of a natural number | Set 1
Modulo 10^9+7 (1000000007)
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Operators in C / C++