

Show HN: an extremely simple program shows a 2D structure in the integers - sage_joch

This seems unlikely to be a &quot;new&quot; idea, but I stumbled into it by accident and thought it was cool. Java source code below:<p><pre><code>  &#x2F;&#x2F; This class is in the public domain.
  public class Numbers
  {
    public static void main(String[] args)
    {
      int numRows = 256;
      int numColumns = 128;
      for (int row = 0; row &lt; numRows; row++)
      { 
        StringBuilder rowOutput = new StringBuilder();
        for (int column = 0; column &lt; numColumns; column++)
        {
          if ((row &amp; column) != 0)
            rowOutput.append(&quot;1&quot;);
          else
            rowOutput.append(&quot;0&quot;);
        }
        System.out.println(
          String.format(&quot;row&#x2F;col %s:\t%s&quot;, row, rowOutput.toString()));
      }
    }
  }</code></pre>
======
psuter
This was on HN a couple of days ago:

    
    
      http://www.oftenpaper.net/sierpinski.htm
    

It points out that you can interpret the table as the relation "is disjoint"
between the bitsets encoded in the row and col indices (CTRL+F for "binary
logic"). Following links from there will take you to a rabbit hole of
combinatorics that I will not claim to fully understand.

The whole page is a delight, though, and given your enjoyment for your
discovery, you'll certainly love browsing it.

------
duncan_bayne
Reminds me of Sierpinski triangles:

[http://en.wikipedia.org/wiki/Sierpinski_triangle](http://en.wikipedia.org/wiki/Sierpinski_triangle)

I'm sure someone with a greater intuition for mathematics than I have will be
able to explain why.

~~~
daxelrod
My first thought is that it has something to do with the fact that you get a
Sierpenski Triangle from a Pascal's triangle when you assign one color to even
numbers and another to odd:
[http://en.wikipedia.org/wiki/Pascal%27s_triangle#Overall_pat...](http://en.wikipedia.org/wiki/Pascal%27s_triangle#Overall_patterns_and_properties)

------
modeless
Here's a C version in less than 80 characters:

    
    
      main(c,r){for(r=32;r;)printf(++c>31?c=!r--,"\n":c<r?" ":~c&r?" `":" #");}
    

I first learned of this phenomenon in my discrete math class, and we had a
competition to see who would write the shortest program to draw a Sierpinski
triangle. This version would certainly have won if I'd been clever enough to
write it at the time, though even shorter versions are possible in other
languages.

------
chch
[http://faculty.msmary.edu/heinold/bitwise_and.pdf](http://faculty.msmary.edu/heinold/bitwise_and.pdf)

seems to be a paper on this phenomenon, with

[http://faculty.msmary.edu/heinold/maa_pres2009-11-14.pdf](http://faculty.msmary.edu/heinold/maa_pres2009-11-14.pdf)

being a similar work in presentation form.

I've never seen it before; I like it!

------
rjbwork
For any lazy C# folks:

    
    
        internal class Program
        {
            public static void Main()
            {
                int numRows = 256;
                int numColumns = 128;
                for (int row = 0; row < numRows; row++)
                {
                    StringBuilder rowOutput = new StringBuilder();
                    for (int column = 0; column < numColumns; column++)
                    {
                        if ((row & column) != 0) rowOutput.Append("1");
                        else rowOutput.Append("0");
                    }
                    Console.WriteLine("row/col {0}:\t{1}", row, rowOutput);
                }
                Console.Read();
            }
        }
    

Cool stuff, very interesting to see that triangle structure emerge.

~~~
parkovski
For the even lazier:
[http://dotnetfiddle.net/gsdfJp](http://dotnetfiddle.net/gsdfJp)

------
zoba
Interesting, I've not seen this before. Here is a screenshot of some of the
output: [http://i.imgur.com/Ro2AA6u.jpg](http://i.imgur.com/Ro2AA6u.jpg)

------
andrewflnr
That's cool. You can expand the pattern, presumably arbitrarily, by setting
the number of columns to 256 or 512 (or greater?), but on my little 1440px
screen I had to redirect to a file and scroll it sideways in an editor to see
the whole thing.

------
vittore
You will probably like searches for rule NNN on wolfram alpha, like
[http://www.wolframalpha.com/input/?i=rule+101](http://www.wolframalpha.com/input/?i=rule+101)

------
LocalMan
Back when they taught me the number line and the Cartesian plane, it all
seemed smooth and straightforward.

In the years since, I've seen primes, rationals, irrationals, Mandelbrot, and
all manner of weird structures popping up out of what seemed like nothing.

How did this happen? And who put all that stuff in there?

------
catmanjan
Sierpinski's Triangles?

------
tunesmith
Is this the same as Rule 90?

