Convert infinite 2D plane integer coords to 1D number











up vote
10
down vote

favorite
4












Say I have an infinte 2D grid (ex. a procedurally generated world) and I want to get a unique number for each integer coordinate pair. How would I accomplish this?



My idea is to use a square spiral, but I cant find a way to make a formula for the unique number other than an algorythm that just goes in a square spiral and stops at the wanted coords.



The application for this converstion could be for example a way to save an n dimensional shape to a file where each line represents a chunk of the shape (by using $u(x, y, z) = u(u(x, y), u(y, z))$ ), or have a very unique random seed for each integer point (ex. a way to hash an integer vector to a data point in an n dimensional array)










share|cite|improve this question






















  • More generally than Ross's answer there are a range of pairing functions, some of which are defined precisely as spirals as you describe. It is a theorem that Ross's answer is the unique quadratic pairing function, modulo exchanging x and y.
    – Robert Frost
    yesterday

















up vote
10
down vote

favorite
4












Say I have an infinte 2D grid (ex. a procedurally generated world) and I want to get a unique number for each integer coordinate pair. How would I accomplish this?



My idea is to use a square spiral, but I cant find a way to make a formula for the unique number other than an algorythm that just goes in a square spiral and stops at the wanted coords.



The application for this converstion could be for example a way to save an n dimensional shape to a file where each line represents a chunk of the shape (by using $u(x, y, z) = u(u(x, y), u(y, z))$ ), or have a very unique random seed for each integer point (ex. a way to hash an integer vector to a data point in an n dimensional array)










share|cite|improve this question






















  • More generally than Ross's answer there are a range of pairing functions, some of which are defined precisely as spirals as you describe. It is a theorem that Ross's answer is the unique quadratic pairing function, modulo exchanging x and y.
    – Robert Frost
    yesterday















up vote
10
down vote

favorite
4









up vote
10
down vote

favorite
4






4





Say I have an infinte 2D grid (ex. a procedurally generated world) and I want to get a unique number for each integer coordinate pair. How would I accomplish this?



My idea is to use a square spiral, but I cant find a way to make a formula for the unique number other than an algorythm that just goes in a square spiral and stops at the wanted coords.



The application for this converstion could be for example a way to save an n dimensional shape to a file where each line represents a chunk of the shape (by using $u(x, y, z) = u(u(x, y), u(y, z))$ ), or have a very unique random seed for each integer point (ex. a way to hash an integer vector to a data point in an n dimensional array)










share|cite|improve this question













Say I have an infinte 2D grid (ex. a procedurally generated world) and I want to get a unique number for each integer coordinate pair. How would I accomplish this?



My idea is to use a square spiral, but I cant find a way to make a formula for the unique number other than an algorythm that just goes in a square spiral and stops at the wanted coords.



The application for this converstion could be for example a way to save an n dimensional shape to a file where each line represents a chunk of the shape (by using $u(x, y, z) = u(u(x, y), u(y, z))$ ), or have a very unique random seed for each integer point (ex. a way to hash an integer vector to a data point in an n dimensional array)







algebraic-geometry






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked 2 days ago









Flutterish

837




837












  • More generally than Ross's answer there are a range of pairing functions, some of which are defined precisely as spirals as you describe. It is a theorem that Ross's answer is the unique quadratic pairing function, modulo exchanging x and y.
    – Robert Frost
    yesterday




















  • More generally than Ross's answer there are a range of pairing functions, some of which are defined precisely as spirals as you describe. It is a theorem that Ross's answer is the unique quadratic pairing function, modulo exchanging x and y.
    – Robert Frost
    yesterday


















More generally than Ross's answer there are a range of pairing functions, some of which are defined precisely as spirals as you describe. It is a theorem that Ross's answer is the unique quadratic pairing function, modulo exchanging x and y.
– Robert Frost
yesterday






More generally than Ross's answer there are a range of pairing functions, some of which are defined precisely as spirals as you describe. It is a theorem that Ross's answer is the unique quadratic pairing function, modulo exchanging x and y.
– Robert Frost
yesterday












4 Answers
4






active

oldest

votes

















up vote
13
down vote



accepted










You need the Cantor pairing function, tuned up to accept integers instead of naturals. The basic function takes a pair of naturals (including zero) $x,y$ and returns a natural $pi(x,y)=frac 12(x+y)(x+y+1)+y$. It is invertible, so given $pi(x,y)$ you can recover $x$ and $y$. Now just take your integers to naturals by $$f(z)=begin {cases} 2z&zge 0\-2z-1& z lt 0end {cases}$$ pair them and you have your result.






share|cite|improve this answer




























    up vote
    8
    down vote













    Other answers state how to convert integers to naturals, I won't repeat this step. Let's suppose you have two naturals, e.g.:



    $$ 123 $$
    $$ 98765 $$



    Add leading zeros to obtain equal number of digits:



    $$ 00123 $$
    $$ 98765 $$



    And "interleave":



    $$ 0908172635 $$



    Reverting is trivial: you pick digits from either odd or even positions.



    Notes:




    • the representation depends on the base of the numeral system you use;

    • you can expand the method to non-negative reals in a quite obvious way;

    • similarly you can create a method that takes any fixed number of numbers and yields one number.






    share|cite|improve this answer























    • This is a valid answer, but I cant see this as an effieicient solution, especially in the 1st example use where one would save the data in the file line with index of the unique number. Also as far as I'm concerned numbers dont begin with 0's, so if this would to be stored in a variable, it would have to be a string which is 256 bits per numerical digit where a numerical variable takes ~3.32 bits per numerical digit. I know this is a math forum, but I'm talking about this because my proposed applications are IT ones!
      – Flutterish
      2 days ago








    • 3




      @Flutterish You need numbers as strings only during conversion. At any other moment you can store numbers as numbers.
      – Kamil Maciorowski
      2 days ago






    • 2




      0908172635, the example you've given is leaded by a zero, which cant be saved as a number. I know I could just cut it off, but then... No, actually yes, you are right, there is no way I will ever end up with 2 leading zeros (except $(0,0)$, but thats not a problem), therefore I will always be able to store it as a number.
      – Flutterish
      2 days ago


















    up vote
    7
    down vote













    There are also tools from number theory. We can first map all integers to non-negative ones, which is easy, just take $$f(n)=left{begin{align}&2n&nge0\&-2n-1&n<0end{align}right.$$ as Ross pointed out. Now us take the pair $(m,n)$ to be $2^{f(m)}3^{f(n)}$. Since we can uniquely decompose positive integers into prime factors, this function is invertible, and you have your result.






    share|cite|improve this answer





















    • Unlike Ross's answer, this answer doesn't biject so some wastage occurs insofar as it will only use two thirds of the integers.
      – Robert Frost
      yesterday






    • 2




      @RobertFrost: In fact, this mapping will skip almost all integers; specifically, its image only includes the powers of 2 and 3 and their products (including 1, as a trivial case), whose asymptomatic density among all integers is zero. For example, the only numbers below 100 this function can return are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81 and 96; they start out pretty dense below 10, but get sparser and sparser quickly.
      – Ilmari Karonen
      yesterday












    • @IlmariKaronen the 3 smooth numbers no less, excluding every number having a 5 rough number other than 1 as a factor.
      – Robert Frost
      yesterday


















    up vote
    4
    down vote













    This is an interesting question. I will provide a method to simplify your algorithm, but not necessarily a formula just yet (I'm sure that what I'm about to show you will lead to a formula...probably).



    Let's begin with a point in the center. That is, we are not starting at the top corner of a semi-infinite plane, we are instead assuming the plane is infinite. The point in the center is assigned the number 1, and we call it the $i = 1$ point. We surround this square with a border of squares. This border has 8 such squares. We repeat the process and get 16 squares. In general, each "border" has $2(2i-1) + 2(2i-3) = 8i - 8 = 8(i-1)$ squares.



    We now want to form a sum of the first $N$ such squares:



    $$S_N = bigg(sum_{i=2}^{N}8(i-1)bigg) + 1$$



    Now, you want to simplify your life, so let's simplify that sum. We would end up with:



    $$S_N = 8bigg(sum_{i=2}^{N}(i-1)bigg) + 1$$



    $$S_N = 8frac{N(N+1)}{2}-1 - (8N - 8) + 1$$ (I'll leave you to simplify) and check my algebra. I'm 99% sure it's accurate.



    Now you want to unpack this. This involves several steps. Say you have the number $M$. You need to find the largest $N$ such that $S_N le M lt S_{N+1}$. Other than a strict search, I'm afraid I don't know how to do that, sorry.



    Once you know the $N$, then you need to compute the quantity $M - S_N$. This quantity tells you how many squares to "walk" from some starting square on border $N+1$ to where you want to be. Since you know where the border squares start, and you know how far you've walked, you know where on the border you are and therefore what the coordinates are.



    The method needs some cleanup, but should do it. Good luck doing that in N-D.






    share|cite|improve this answer























      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "69"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      noCode: true, onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3003672%2fconvert-infinite-2d-plane-integer-coords-to-1d-number%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      13
      down vote



      accepted










      You need the Cantor pairing function, tuned up to accept integers instead of naturals. The basic function takes a pair of naturals (including zero) $x,y$ and returns a natural $pi(x,y)=frac 12(x+y)(x+y+1)+y$. It is invertible, so given $pi(x,y)$ you can recover $x$ and $y$. Now just take your integers to naturals by $$f(z)=begin {cases} 2z&zge 0\-2z-1& z lt 0end {cases}$$ pair them and you have your result.






      share|cite|improve this answer

























        up vote
        13
        down vote



        accepted










        You need the Cantor pairing function, tuned up to accept integers instead of naturals. The basic function takes a pair of naturals (including zero) $x,y$ and returns a natural $pi(x,y)=frac 12(x+y)(x+y+1)+y$. It is invertible, so given $pi(x,y)$ you can recover $x$ and $y$. Now just take your integers to naturals by $$f(z)=begin {cases} 2z&zge 0\-2z-1& z lt 0end {cases}$$ pair them and you have your result.






        share|cite|improve this answer























          up vote
          13
          down vote



          accepted







          up vote
          13
          down vote



          accepted






          You need the Cantor pairing function, tuned up to accept integers instead of naturals. The basic function takes a pair of naturals (including zero) $x,y$ and returns a natural $pi(x,y)=frac 12(x+y)(x+y+1)+y$. It is invertible, so given $pi(x,y)$ you can recover $x$ and $y$. Now just take your integers to naturals by $$f(z)=begin {cases} 2z&zge 0\-2z-1& z lt 0end {cases}$$ pair them and you have your result.






          share|cite|improve this answer












          You need the Cantor pairing function, tuned up to accept integers instead of naturals. The basic function takes a pair of naturals (including zero) $x,y$ and returns a natural $pi(x,y)=frac 12(x+y)(x+y+1)+y$. It is invertible, so given $pi(x,y)$ you can recover $x$ and $y$. Now just take your integers to naturals by $$f(z)=begin {cases} 2z&zge 0\-2z-1& z lt 0end {cases}$$ pair them and you have your result.







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered 2 days ago









          Ross Millikan

          287k23195364




          287k23195364






















              up vote
              8
              down vote













              Other answers state how to convert integers to naturals, I won't repeat this step. Let's suppose you have two naturals, e.g.:



              $$ 123 $$
              $$ 98765 $$



              Add leading zeros to obtain equal number of digits:



              $$ 00123 $$
              $$ 98765 $$



              And "interleave":



              $$ 0908172635 $$



              Reverting is trivial: you pick digits from either odd or even positions.



              Notes:




              • the representation depends on the base of the numeral system you use;

              • you can expand the method to non-negative reals in a quite obvious way;

              • similarly you can create a method that takes any fixed number of numbers and yields one number.






              share|cite|improve this answer























              • This is a valid answer, but I cant see this as an effieicient solution, especially in the 1st example use where one would save the data in the file line with index of the unique number. Also as far as I'm concerned numbers dont begin with 0's, so if this would to be stored in a variable, it would have to be a string which is 256 bits per numerical digit where a numerical variable takes ~3.32 bits per numerical digit. I know this is a math forum, but I'm talking about this because my proposed applications are IT ones!
                – Flutterish
                2 days ago








              • 3




                @Flutterish You need numbers as strings only during conversion. At any other moment you can store numbers as numbers.
                – Kamil Maciorowski
                2 days ago






              • 2




                0908172635, the example you've given is leaded by a zero, which cant be saved as a number. I know I could just cut it off, but then... No, actually yes, you are right, there is no way I will ever end up with 2 leading zeros (except $(0,0)$, but thats not a problem), therefore I will always be able to store it as a number.
                – Flutterish
                2 days ago















              up vote
              8
              down vote













              Other answers state how to convert integers to naturals, I won't repeat this step. Let's suppose you have two naturals, e.g.:



              $$ 123 $$
              $$ 98765 $$



              Add leading zeros to obtain equal number of digits:



              $$ 00123 $$
              $$ 98765 $$



              And "interleave":



              $$ 0908172635 $$



              Reverting is trivial: you pick digits from either odd or even positions.



              Notes:




              • the representation depends on the base of the numeral system you use;

              • you can expand the method to non-negative reals in a quite obvious way;

              • similarly you can create a method that takes any fixed number of numbers and yields one number.






              share|cite|improve this answer























              • This is a valid answer, but I cant see this as an effieicient solution, especially in the 1st example use where one would save the data in the file line with index of the unique number. Also as far as I'm concerned numbers dont begin with 0's, so if this would to be stored in a variable, it would have to be a string which is 256 bits per numerical digit where a numerical variable takes ~3.32 bits per numerical digit. I know this is a math forum, but I'm talking about this because my proposed applications are IT ones!
                – Flutterish
                2 days ago








              • 3




                @Flutterish You need numbers as strings only during conversion. At any other moment you can store numbers as numbers.
                – Kamil Maciorowski
                2 days ago






              • 2




                0908172635, the example you've given is leaded by a zero, which cant be saved as a number. I know I could just cut it off, but then... No, actually yes, you are right, there is no way I will ever end up with 2 leading zeros (except $(0,0)$, but thats not a problem), therefore I will always be able to store it as a number.
                – Flutterish
                2 days ago













              up vote
              8
              down vote










              up vote
              8
              down vote









              Other answers state how to convert integers to naturals, I won't repeat this step. Let's suppose you have two naturals, e.g.:



              $$ 123 $$
              $$ 98765 $$



              Add leading zeros to obtain equal number of digits:



              $$ 00123 $$
              $$ 98765 $$



              And "interleave":



              $$ 0908172635 $$



              Reverting is trivial: you pick digits from either odd or even positions.



              Notes:




              • the representation depends on the base of the numeral system you use;

              • you can expand the method to non-negative reals in a quite obvious way;

              • similarly you can create a method that takes any fixed number of numbers and yields one number.






              share|cite|improve this answer














              Other answers state how to convert integers to naturals, I won't repeat this step. Let's suppose you have two naturals, e.g.:



              $$ 123 $$
              $$ 98765 $$



              Add leading zeros to obtain equal number of digits:



              $$ 00123 $$
              $$ 98765 $$



              And "interleave":



              $$ 0908172635 $$



              Reverting is trivial: you pick digits from either odd or even positions.



              Notes:




              • the representation depends on the base of the numeral system you use;

              • you can expand the method to non-negative reals in a quite obvious way;

              • similarly you can create a method that takes any fixed number of numbers and yields one number.







              share|cite|improve this answer














              share|cite|improve this answer



              share|cite|improve this answer








              edited 2 days ago

























              answered 2 days ago









              Kamil Maciorowski

              2,4691920




              2,4691920












              • This is a valid answer, but I cant see this as an effieicient solution, especially in the 1st example use where one would save the data in the file line with index of the unique number. Also as far as I'm concerned numbers dont begin with 0's, so if this would to be stored in a variable, it would have to be a string which is 256 bits per numerical digit where a numerical variable takes ~3.32 bits per numerical digit. I know this is a math forum, but I'm talking about this because my proposed applications are IT ones!
                – Flutterish
                2 days ago








              • 3




                @Flutterish You need numbers as strings only during conversion. At any other moment you can store numbers as numbers.
                – Kamil Maciorowski
                2 days ago






              • 2




                0908172635, the example you've given is leaded by a zero, which cant be saved as a number. I know I could just cut it off, but then... No, actually yes, you are right, there is no way I will ever end up with 2 leading zeros (except $(0,0)$, but thats not a problem), therefore I will always be able to store it as a number.
                – Flutterish
                2 days ago


















              • This is a valid answer, but I cant see this as an effieicient solution, especially in the 1st example use where one would save the data in the file line with index of the unique number. Also as far as I'm concerned numbers dont begin with 0's, so if this would to be stored in a variable, it would have to be a string which is 256 bits per numerical digit where a numerical variable takes ~3.32 bits per numerical digit. I know this is a math forum, but I'm talking about this because my proposed applications are IT ones!
                – Flutterish
                2 days ago








              • 3




                @Flutterish You need numbers as strings only during conversion. At any other moment you can store numbers as numbers.
                – Kamil Maciorowski
                2 days ago






              • 2




                0908172635, the example you've given is leaded by a zero, which cant be saved as a number. I know I could just cut it off, but then... No, actually yes, you are right, there is no way I will ever end up with 2 leading zeros (except $(0,0)$, but thats not a problem), therefore I will always be able to store it as a number.
                – Flutterish
                2 days ago
















              This is a valid answer, but I cant see this as an effieicient solution, especially in the 1st example use where one would save the data in the file line with index of the unique number. Also as far as I'm concerned numbers dont begin with 0's, so if this would to be stored in a variable, it would have to be a string which is 256 bits per numerical digit where a numerical variable takes ~3.32 bits per numerical digit. I know this is a math forum, but I'm talking about this because my proposed applications are IT ones!
              – Flutterish
              2 days ago






              This is a valid answer, but I cant see this as an effieicient solution, especially in the 1st example use where one would save the data in the file line with index of the unique number. Also as far as I'm concerned numbers dont begin with 0's, so if this would to be stored in a variable, it would have to be a string which is 256 bits per numerical digit where a numerical variable takes ~3.32 bits per numerical digit. I know this is a math forum, but I'm talking about this because my proposed applications are IT ones!
              – Flutterish
              2 days ago






              3




              3




              @Flutterish You need numbers as strings only during conversion. At any other moment you can store numbers as numbers.
              – Kamil Maciorowski
              2 days ago




              @Flutterish You need numbers as strings only during conversion. At any other moment you can store numbers as numbers.
              – Kamil Maciorowski
              2 days ago




              2




              2




              0908172635, the example you've given is leaded by a zero, which cant be saved as a number. I know I could just cut it off, but then... No, actually yes, you are right, there is no way I will ever end up with 2 leading zeros (except $(0,0)$, but thats not a problem), therefore I will always be able to store it as a number.
              – Flutterish
              2 days ago




              0908172635, the example you've given is leaded by a zero, which cant be saved as a number. I know I could just cut it off, but then... No, actually yes, you are right, there is no way I will ever end up with 2 leading zeros (except $(0,0)$, but thats not a problem), therefore I will always be able to store it as a number.
              – Flutterish
              2 days ago










              up vote
              7
              down vote













              There are also tools from number theory. We can first map all integers to non-negative ones, which is easy, just take $$f(n)=left{begin{align}&2n&nge0\&-2n-1&n<0end{align}right.$$ as Ross pointed out. Now us take the pair $(m,n)$ to be $2^{f(m)}3^{f(n)}$. Since we can uniquely decompose positive integers into prime factors, this function is invertible, and you have your result.






              share|cite|improve this answer





















              • Unlike Ross's answer, this answer doesn't biject so some wastage occurs insofar as it will only use two thirds of the integers.
                – Robert Frost
                yesterday






              • 2




                @RobertFrost: In fact, this mapping will skip almost all integers; specifically, its image only includes the powers of 2 and 3 and their products (including 1, as a trivial case), whose asymptomatic density among all integers is zero. For example, the only numbers below 100 this function can return are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81 and 96; they start out pretty dense below 10, but get sparser and sparser quickly.
                – Ilmari Karonen
                yesterday












              • @IlmariKaronen the 3 smooth numbers no less, excluding every number having a 5 rough number other than 1 as a factor.
                – Robert Frost
                yesterday















              up vote
              7
              down vote













              There are also tools from number theory. We can first map all integers to non-negative ones, which is easy, just take $$f(n)=left{begin{align}&2n&nge0\&-2n-1&n<0end{align}right.$$ as Ross pointed out. Now us take the pair $(m,n)$ to be $2^{f(m)}3^{f(n)}$. Since we can uniquely decompose positive integers into prime factors, this function is invertible, and you have your result.






              share|cite|improve this answer





















              • Unlike Ross's answer, this answer doesn't biject so some wastage occurs insofar as it will only use two thirds of the integers.
                – Robert Frost
                yesterday






              • 2




                @RobertFrost: In fact, this mapping will skip almost all integers; specifically, its image only includes the powers of 2 and 3 and their products (including 1, as a trivial case), whose asymptomatic density among all integers is zero. For example, the only numbers below 100 this function can return are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81 and 96; they start out pretty dense below 10, but get sparser and sparser quickly.
                – Ilmari Karonen
                yesterday












              • @IlmariKaronen the 3 smooth numbers no less, excluding every number having a 5 rough number other than 1 as a factor.
                – Robert Frost
                yesterday













              up vote
              7
              down vote










              up vote
              7
              down vote









              There are also tools from number theory. We can first map all integers to non-negative ones, which is easy, just take $$f(n)=left{begin{align}&2n&nge0\&-2n-1&n<0end{align}right.$$ as Ross pointed out. Now us take the pair $(m,n)$ to be $2^{f(m)}3^{f(n)}$. Since we can uniquely decompose positive integers into prime factors, this function is invertible, and you have your result.






              share|cite|improve this answer












              There are also tools from number theory. We can first map all integers to non-negative ones, which is easy, just take $$f(n)=left{begin{align}&2n&nge0\&-2n-1&n<0end{align}right.$$ as Ross pointed out. Now us take the pair $(m,n)$ to be $2^{f(m)}3^{f(n)}$. Since we can uniquely decompose positive integers into prime factors, this function is invertible, and you have your result.







              share|cite|improve this answer












              share|cite|improve this answer



              share|cite|improve this answer










              answered 2 days ago









              Trebor

              54912




              54912












              • Unlike Ross's answer, this answer doesn't biject so some wastage occurs insofar as it will only use two thirds of the integers.
                – Robert Frost
                yesterday






              • 2




                @RobertFrost: In fact, this mapping will skip almost all integers; specifically, its image only includes the powers of 2 and 3 and their products (including 1, as a trivial case), whose asymptomatic density among all integers is zero. For example, the only numbers below 100 this function can return are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81 and 96; they start out pretty dense below 10, but get sparser and sparser quickly.
                – Ilmari Karonen
                yesterday












              • @IlmariKaronen the 3 smooth numbers no less, excluding every number having a 5 rough number other than 1 as a factor.
                – Robert Frost
                yesterday


















              • Unlike Ross's answer, this answer doesn't biject so some wastage occurs insofar as it will only use two thirds of the integers.
                – Robert Frost
                yesterday






              • 2




                @RobertFrost: In fact, this mapping will skip almost all integers; specifically, its image only includes the powers of 2 and 3 and their products (including 1, as a trivial case), whose asymptomatic density among all integers is zero. For example, the only numbers below 100 this function can return are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81 and 96; they start out pretty dense below 10, but get sparser and sparser quickly.
                – Ilmari Karonen
                yesterday












              • @IlmariKaronen the 3 smooth numbers no less, excluding every number having a 5 rough number other than 1 as a factor.
                – Robert Frost
                yesterday
















              Unlike Ross's answer, this answer doesn't biject so some wastage occurs insofar as it will only use two thirds of the integers.
              – Robert Frost
              yesterday




              Unlike Ross's answer, this answer doesn't biject so some wastage occurs insofar as it will only use two thirds of the integers.
              – Robert Frost
              yesterday




              2




              2




              @RobertFrost: In fact, this mapping will skip almost all integers; specifically, its image only includes the powers of 2 and 3 and their products (including 1, as a trivial case), whose asymptomatic density among all integers is zero. For example, the only numbers below 100 this function can return are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81 and 96; they start out pretty dense below 10, but get sparser and sparser quickly.
              – Ilmari Karonen
              yesterday






              @RobertFrost: In fact, this mapping will skip almost all integers; specifically, its image only includes the powers of 2 and 3 and their products (including 1, as a trivial case), whose asymptomatic density among all integers is zero. For example, the only numbers below 100 this function can return are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81 and 96; they start out pretty dense below 10, but get sparser and sparser quickly.
              – Ilmari Karonen
              yesterday














              @IlmariKaronen the 3 smooth numbers no less, excluding every number having a 5 rough number other than 1 as a factor.
              – Robert Frost
              yesterday




              @IlmariKaronen the 3 smooth numbers no less, excluding every number having a 5 rough number other than 1 as a factor.
              – Robert Frost
              yesterday










              up vote
              4
              down vote













              This is an interesting question. I will provide a method to simplify your algorithm, but not necessarily a formula just yet (I'm sure that what I'm about to show you will lead to a formula...probably).



              Let's begin with a point in the center. That is, we are not starting at the top corner of a semi-infinite plane, we are instead assuming the plane is infinite. The point in the center is assigned the number 1, and we call it the $i = 1$ point. We surround this square with a border of squares. This border has 8 such squares. We repeat the process and get 16 squares. In general, each "border" has $2(2i-1) + 2(2i-3) = 8i - 8 = 8(i-1)$ squares.



              We now want to form a sum of the first $N$ such squares:



              $$S_N = bigg(sum_{i=2}^{N}8(i-1)bigg) + 1$$



              Now, you want to simplify your life, so let's simplify that sum. We would end up with:



              $$S_N = 8bigg(sum_{i=2}^{N}(i-1)bigg) + 1$$



              $$S_N = 8frac{N(N+1)}{2}-1 - (8N - 8) + 1$$ (I'll leave you to simplify) and check my algebra. I'm 99% sure it's accurate.



              Now you want to unpack this. This involves several steps. Say you have the number $M$. You need to find the largest $N$ such that $S_N le M lt S_{N+1}$. Other than a strict search, I'm afraid I don't know how to do that, sorry.



              Once you know the $N$, then you need to compute the quantity $M - S_N$. This quantity tells you how many squares to "walk" from some starting square on border $N+1$ to where you want to be. Since you know where the border squares start, and you know how far you've walked, you know where on the border you are and therefore what the coordinates are.



              The method needs some cleanup, but should do it. Good luck doing that in N-D.






              share|cite|improve this answer



























                up vote
                4
                down vote













                This is an interesting question. I will provide a method to simplify your algorithm, but not necessarily a formula just yet (I'm sure that what I'm about to show you will lead to a formula...probably).



                Let's begin with a point in the center. That is, we are not starting at the top corner of a semi-infinite plane, we are instead assuming the plane is infinite. The point in the center is assigned the number 1, and we call it the $i = 1$ point. We surround this square with a border of squares. This border has 8 such squares. We repeat the process and get 16 squares. In general, each "border" has $2(2i-1) + 2(2i-3) = 8i - 8 = 8(i-1)$ squares.



                We now want to form a sum of the first $N$ such squares:



                $$S_N = bigg(sum_{i=2}^{N}8(i-1)bigg) + 1$$



                Now, you want to simplify your life, so let's simplify that sum. We would end up with:



                $$S_N = 8bigg(sum_{i=2}^{N}(i-1)bigg) + 1$$



                $$S_N = 8frac{N(N+1)}{2}-1 - (8N - 8) + 1$$ (I'll leave you to simplify) and check my algebra. I'm 99% sure it's accurate.



                Now you want to unpack this. This involves several steps. Say you have the number $M$. You need to find the largest $N$ such that $S_N le M lt S_{N+1}$. Other than a strict search, I'm afraid I don't know how to do that, sorry.



                Once you know the $N$, then you need to compute the quantity $M - S_N$. This quantity tells you how many squares to "walk" from some starting square on border $N+1$ to where you want to be. Since you know where the border squares start, and you know how far you've walked, you know where on the border you are and therefore what the coordinates are.



                The method needs some cleanup, but should do it. Good luck doing that in N-D.






                share|cite|improve this answer

























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  This is an interesting question. I will provide a method to simplify your algorithm, but not necessarily a formula just yet (I'm sure that what I'm about to show you will lead to a formula...probably).



                  Let's begin with a point in the center. That is, we are not starting at the top corner of a semi-infinite plane, we are instead assuming the plane is infinite. The point in the center is assigned the number 1, and we call it the $i = 1$ point. We surround this square with a border of squares. This border has 8 such squares. We repeat the process and get 16 squares. In general, each "border" has $2(2i-1) + 2(2i-3) = 8i - 8 = 8(i-1)$ squares.



                  We now want to form a sum of the first $N$ such squares:



                  $$S_N = bigg(sum_{i=2}^{N}8(i-1)bigg) + 1$$



                  Now, you want to simplify your life, so let's simplify that sum. We would end up with:



                  $$S_N = 8bigg(sum_{i=2}^{N}(i-1)bigg) + 1$$



                  $$S_N = 8frac{N(N+1)}{2}-1 - (8N - 8) + 1$$ (I'll leave you to simplify) and check my algebra. I'm 99% sure it's accurate.



                  Now you want to unpack this. This involves several steps. Say you have the number $M$. You need to find the largest $N$ such that $S_N le M lt S_{N+1}$. Other than a strict search, I'm afraid I don't know how to do that, sorry.



                  Once you know the $N$, then you need to compute the quantity $M - S_N$. This quantity tells you how many squares to "walk" from some starting square on border $N+1$ to where you want to be. Since you know where the border squares start, and you know how far you've walked, you know where on the border you are and therefore what the coordinates are.



                  The method needs some cleanup, but should do it. Good luck doing that in N-D.






                  share|cite|improve this answer














                  This is an interesting question. I will provide a method to simplify your algorithm, but not necessarily a formula just yet (I'm sure that what I'm about to show you will lead to a formula...probably).



                  Let's begin with a point in the center. That is, we are not starting at the top corner of a semi-infinite plane, we are instead assuming the plane is infinite. The point in the center is assigned the number 1, and we call it the $i = 1$ point. We surround this square with a border of squares. This border has 8 such squares. We repeat the process and get 16 squares. In general, each "border" has $2(2i-1) + 2(2i-3) = 8i - 8 = 8(i-1)$ squares.



                  We now want to form a sum of the first $N$ such squares:



                  $$S_N = bigg(sum_{i=2}^{N}8(i-1)bigg) + 1$$



                  Now, you want to simplify your life, so let's simplify that sum. We would end up with:



                  $$S_N = 8bigg(sum_{i=2}^{N}(i-1)bigg) + 1$$



                  $$S_N = 8frac{N(N+1)}{2}-1 - (8N - 8) + 1$$ (I'll leave you to simplify) and check my algebra. I'm 99% sure it's accurate.



                  Now you want to unpack this. This involves several steps. Say you have the number $M$. You need to find the largest $N$ such that $S_N le M lt S_{N+1}$. Other than a strict search, I'm afraid I don't know how to do that, sorry.



                  Once you know the $N$, then you need to compute the quantity $M - S_N$. This quantity tells you how many squares to "walk" from some starting square on border $N+1$ to where you want to be. Since you know where the border squares start, and you know how far you've walked, you know where on the border you are and therefore what the coordinates are.



                  The method needs some cleanup, but should do it. Good luck doing that in N-D.







                  share|cite|improve this answer














                  share|cite|improve this answer



                  share|cite|improve this answer








                  edited 2 days ago

























                  answered 2 days ago









                  Michael Stachowsky

                  1,183417




                  1,183417






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3003672%2fconvert-infinite-2d-plane-integer-coords-to-1d-number%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]