[Question]
  [
Given a number **n**, generate the first **n** columns of this pattern:
```
                               #
               #               #
       #       #       #       #
   #   #   #   #   #   #   #   #
 # # # # # # # # # # # # # # # #
################################ ...
```
The height of the column at (1-indexed) **n** is the number of trailing `0` bits in **n**'s binary representation, plus one. As a result, the bottom layer has every column filled in, the second layer every second column, the third layer every fourth column, etc.
## Rules
* You may input and output through any standard method.
* You may assume the input is an integer between 1 and 999, inclusive.
* The output may contain any amount of whitespace, as long as the pattern is intact.
* The pattern must be 1-indexed, and in the same format as shown here.
* You may use any single non-whitespace character in place of `#`, but you may not change the space character.
## Test cases
```
1
#
2
 #
##
3
 # 
###
4
   #
 # #
####
5
   # 
 # # 
#####
7
   #   
 # # # 
#######
32
                               #
               #               #
       #       #       #       #
   #   #   #   #   #   #   #   #
 # # # # # # # # # # # # # # # #
################################
```
A few larger test cases can be found [here](https://gist.github.com/ETHproductions/96417705cfb7c3b851d2fb3ff2829092).
## Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so **the shortest code in bytes in each language wins.**
  
[Answer]
# [Python 2](https://docs.python.org/2/), 54 bytes
```
i=n=input()
while i:i-=1;print((' '*~-2**i+'#')*n)[:n]
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P9M2zzYzr6C0REOTqzwjMydVIdMqU9fW0LqgKDOvRENDXUFdq07XSEsrU1tdWV1TK08z2iov9v9/QwMA "Python 2 – Try It Online")
Prints with lots of leading whitespace. Each row `i` counting down from `n` repeats a pattern of `2**i-1` spaces followed by a `#`. This pattern is repeated up to the width of the ruler, which is the input `n`. This is done by multiplying the pattern string by `n` and taking the first `n` characters with `[:n]`.
The pattern can be made by string formatting for an equal length alternative.
```
i=n=input()
while i:i-=1;print('%%%ds'%2**i%'#'*n)[:n]
```
A cute slicing method is longer.
```
n=input();s=~-2**n*' '+'#'
exec"s=s[1::2]*2;print s[:n];"*n
```
[Answer]
# [Python 3](https://docs.python.org/3/), 74 bytes
```
n=int(input())
a=1
while a<n:a*=2
while a:print(("%%%dd"%a%4*n)[:n]);a//=2
```
[Try it online!](https://tio.run/##K6gsycjPM/7/P882M69EIzOvoLREQ1OTK9HWkKs8IzMnVSHRJs8qUcvWCMa1KigCqdRQUlVVTUlRUk1UNdHK04y2yovVtE7U17c1@v/f2AgA "Python 3 – Try It Online")
[Answer]
# [V](https://github.com/DJMcMayhem/V), ~~17~~, 16 bytes
```
é#Àñä}Är {ñÎÀlD
```
[Try it online!](https://tio.run/##K/v///BK5cMNhzceXiJWe7ilSKEayOw73JDj8v//f3MA "V – Try It Online")
Hexdump: 
```
00000000: e923 c0f1 e416 7dc4 7220 7bf1 cec0 6c44  .#....}.r {...lD
```
*Thanks to @KritixiLithos for saving one byte!*
This algorithm is *horribly* inefficient, but it should work in theory for any size input. 
It works by generating the first *n* iterations of the following pattern:
```
#
 #
##
   #
 # #
####
       #
   #   #
 # # # #
########
               #
       #       #
   #   #   #   #
 # # # # # # # #
################
                               #
               #               #
       #       #       #       #
   #   #   #   #   #   #   #   #
 # # # # # # # # # # # # # # # #
################################
```
And then chopping off all but the first *n* columns. As such, this will produce a ton of leading whitespace, but the OP said:
> 
> The output may contain any amount of whitespace, as long as the pattern is intact
> 
> 
> 
Explanation:
```
é#                      " Insert an '#'
  Àñ           ñ        " 'N' times:
    ä<C-v>}             "   Duplicate every line blockwise (duplicating horizontally instead of vertically)
           Ä            "   Duplicate the top line. This conveniently puts us on the first non-whitespace character (that is, '#')
            r           "   Replace this character with a space
              {         "   Move to the beginning of the buffer
                Î       " On every line:
                 Àl     "   Move 'N' characters to the right ('l' for right, makes sense, right?)
                   D    "   And delete everything after the cursor
```
[Answer]
# JavaScript (ES6), ~~61~~ 58 bytes
```
f=(n,c=n,s='')=>c?f(n,c>>1,s+s+' ')+`
`+(s+1).repeat(c):''
```
*Saved 1 byte thanks to @ETHProductions, then 2 more bytes when I saw that any character could be used.*
A recursive solution.
**Test cases:**
```
f=(n,c=n,s='')=>c?f(n,c>>1,s+s+' ')+`
`+(s+1).repeat(c):''
console.log(f(32));
console.log(f(1));  
console.log(f(5));  
console.log(f(7));  
```
**Animation:**
```
f=(n,c=n,s='')=>c?f(n,c>>1,s+s+' ')+`
`+(s+1).repeat(c):''
for(let i = 1 ; i < 64 ; i++) {
  setTimeout(_=>document.querySelector('pre').textContent=f(i), i*40);
}
```
```
<pre>
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 21 bytes
```
'# '[1+⊖0⍪∨⍀⊖2⊥⍣¯1⍳⎕]
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKO9qDQntei/urKCerSh9qOuaQaPelc96ljxqLcByDF61LX0Ue/iQ@sNH/VuftQ3NRak4z9YC5exEQA "APL (Dyalog Unicode) – Try It Online")
`'# '[`…`] index the string with
 `⎕` get input
 `⍳` that many **i**ntegers
 `2⊥⍣¯1` convert to binary, using as many digits as needed (one number in each *column*)
 `⊖` flip upside down
 `∨⍀` vertical cumulative OR reduction
 `0⍪` concatenate zeros on top
 `⊖` flip upside down (i.e. back up again)
 `1+` add one (for 1-based indexing)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~11~~ 10 bytes
```
Rọ2‘4ẋz⁶ṚY
```
[Try it online!](https://tio.run/##ASAA3/9qZWxsef//UuG7jTLigJg04bqLeuKBtuG5mln///8zMg "Jelly – Try It Online")
1 byte saved after the OP added a relaxation that the character doesn't have to be `#`.
[Answer]
# Octave, 45 bytes
```
@(n)[[sort(cummin(de2bi(g=0:n)'));g|1]+32 '']
```
Try it on [Octave Online!](http://octave-online.net/#cmd=(%40(n)%5B%5Bsort(cummin(de2bi(g%3D0%3An)%27))%3Bg%7C1%5D%2B32%20%27%27%5D)(20))
Instead of `'#'` prints `'!'`.
[Answer]
# [PHP](https://php.net/), 139 bytes
```
for($f=array_fill(0,$l=1+log($a=$argn,2)," ");$n++<$a;)for($h=1+strspn(strrev(decbin($n)),$i=0);$i<$h;)$f[$l-++$i][$n]=3;echo join("
",$f);
```
[Try it online!](https://tio.run/##HctBCsMgFEXReVcR5A38aCFNhka6kBCKTWO0iIopha7eSkZ3cm52uU737HJ3gSl71OOgqk2Fw2pTivk9rA@B9xJB30RIO4fRp5QDSdYxUohCTDCKzs01dnzKkSNvKduXv7b16SNHJJLwum@Hn@AUwc4IVyHglxlx0aPaVpe6d2qaXZiEJVXrHw "PHP – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 20 17 bytes
*Saved 3 bytes thanks to @Shaggy and @ETHproductions*
```
õ_¤q1 o Ä çoÃ·z w
```
[Try it online!](https://tio.run/##y0osKPn///DW@ENLCg0V8hUOtygcXp5/uPnQ9iqF8v//jY0A "Japt – Try It Online")
## Explanation:
Input: 5
```
õ_¤q1 o Ä çoÃ·z w
õ           Ã       // Create a range [1...Input] [1,2,3,4,5]
 _                  // Map; At each item:
  ¤                 //   Convert to binary        ["1","10","11","100","101"]
   q1               //   Split on "1"             [["",""],["","0"],["","",""],["","00"],["","0",""]]
      o             //   Get the last item        ["","0","","00",""]
        Ä           //   Add 1                    [["1","01","1","001","1"]]
          ço        //   Fill with "o"            ["o","oo","o","ooo","o"]
             ·      // Join with new-lines        ["o\noo\no\nooo\no"]
              z     // Rotate 90 degrees          ["ooooo\n o o \n o   "]
                w   // Reverse                    ["   o \n o o \nooooo"]
```
[Answer]
# C, ~~84~~ 74 bytes
```
f(i,l,m){putchar(32+3*!(i&m));i<l?f(i+1,l,m):m?putchar(10),f(1,l,m>>1):1;}
```
### Ungolfed:
```
void f(int counter, int length, int mask) {
    putchar((counter&mask) ? ' ' : '#');
    if(counter<length) {
        f(counter+1, length, mask);
    } else if(mask) {
        putchar('\n');
        f(1, length, mask>>1);
    }
}
```
### Test with:
```
int main() {
    f(1, 32, 1023);
    putchar('\n');
    f(1, 1, 1023);
    putchar('\n');
    f(1, 999, 1023);
    putchar('\n');
}
```
### Explanation
Once again, recursion takes less characters in C than iteration, so the two loops are expressed as the two recursional invocations.
Also, C is a great language for playing tricks with boolean expressions, allowing the decision of whether to put a blank or a `#` to be expressed by the expression `32+3*!(i&m)`. A space has the ASCII value of 32, the `#` is ASCII 35, so we get a blank if any of the bits in the mask is set in `i`.
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 15 Bytes
```
j_.tm*Nhx_.Bd1S
```
[Try it!](https://pyth.herokuapp.com/?code=j_.tm%2aNhx_.Bd1S&input=70&debug=0)
### explanation
```
j_.tm*Nhx_.Bd1S
    m         SQ   # map over the numbers from 0 to the implicit input (lambda variable: d)
          .Bd      # Convert d to a binary string: (12 -> 1100)
         _         # reverse: (1100 -> 0011)
        x    1     # get the location of the first 1 ( 2 )
     *Nh           # make one more than that " quotation marks (""")
 _.t               # transpose the list of quotation mark strings and reverse it
j                  # join on newline
    
```
[Answer]
# [Python 2](https://docs.python.org/2/), 47 bytes
```
f=lambda n:n and' '.join(f(n/2))+'\n'+n*'#'or''
```
[Try it online!](https://tio.run/##DcvRCkAwFAbgVznl4hgnMqSUJ8HFpGXi31puPP347r/wPoeHTslOl7m33RBGkMHOxNXpHXKbo9ZKlbyASxScsY/MyfpIIAeaG9HSSie9DNLqdaQQHR6C/FOlDw "Python 2 – Try It Online")
[Answer]
# JavaScript (ES8), 71 bytes
The [padStart()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart) function was introduced in ECMAScript 2017!
```
N=>eval(`for(s='',n=1;n<=N;n*=2)s='\\n'+'#'.padStart(n).repeat(N/n)+s`)
```
```
f=
N=>eval(`for(s='',n=1;n<=N;n*=2)s='\\n'+'#'.padStart(n).repeat(N/n)+s`)
console.log(f(+prompt()))
```
---
# JavaScript (ES6), 77 bytes
```
N=>eval(`for(s='',n=1;n<=N;n*=2)s='\\n'+(' '.repeat(n-1)+'#').repeat(N/n)+s`)
```
```
f=
N=>eval(`for(s='',n=1;n<=N;n*=2)s='\\n'+(' '.repeat(n-1)+'#').repeat(N/n)+s`)
console.log(f(+prompt()))
```
[Answer]
# Mathematica, 69 bytes
```
Rotate[Grid["#"~Table~#&/@(IntegerExponent[2*#,2]&/@Range[#])],Pi/2]&
```
[Answer]
## ([WESRRMICGSE](https://codegolf.stackexchange.com/a/102566/60951)): 237 bytes
```
IF(ROW()<=FLOOR(LOG(COUNTA(R1C:R[-1]C,R[1]C:R[1024]C)+1,2),1)+2,LEFT(REPT(REPT(" ",FLOOR(POWER(2,LOG(COUNTA(R1C:R[-1]C,R[1]C:R[1024]C)+1,2)-ROW()+2),1)-1) &"#",COUNTA(R1C:R[-1]C,R[1]C:R[1024]C)+1),COUNTA(R1C:R[-1]C,R[1]C:R[1024]C)+1),"")
```
Alright. 'splaining time.
First off, replace every `COUNTA(R1C:R[-1]C,R[1]C:R[1024]C)+1` with simply `[i]`, for input. the token counts the number of cells, not including itself, that contain a formula, and then adds one, to include itself. Since WESRRMICGSE drags a formula according to the input you give it, this token always results in the input.
we have:
```
IF(ROW()<=FLOOR(LOG([i],2),1)+3,LEFT(REPT(REPT(" ",FLOOR(POWER(2,LOG([i],2)-ROW()+2),1)-1) &"#",[i]),[i]),"")
```
This is much more readable.
You're going to see the `FLOOR(LOG([i],2),1)` token a lot, which simply means take the nearest power of 2 which is less than the input (`[i]`) number. eg: `4->4, 5->4, 6->4, 7->4, 8->8 ...etc`. I'll replace that with `GS[[i]]`
```
IF(ROW()<=GS[[i]]+3,LEFT(REPT(REPT(" ",,FLOOR(POWER(2,LOG([i],2)-ROW()+2),1),1)-1) &"#",[i]),[i]),"")
```
better. breaking down the if clause, we're testing if the row is less than or equal to `GS[[i]]+3`, because all rulers' height is equal to the GS[[i]]+1, this selects the rows which are equal to the height of the ruler. `+1` for 1-indexing rows, and `+1` again for WESRRMICGSE offset.
The `FALSE` result yields an empty cell (""), and a true result yields `LEFT(REPT(REPT(" ",,FLOOR(POWER(2,LOG([i],2)-ROW()+2),1),1)-1) &"#",[i]),[i])`
currently still editing, stay tuned
[Answer]
# [Haskell](https://www.haskell.org/), ~~64~~ 62 bytes
```
l m=([2..2^m]>>" ")++'#':l m
f n=unlines$take n.l<$>[n,n-1..0]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P0ch11Yj2khPzyguN9bOTklBSVNbW11Z3QoowZWmkGdbmpeTmZdarFKSmJ2qkKeXY6NiF52nk6drqKdnEPs/NzEzT8FWoaC0JLikSEFFIU3B2Og/AA "Haskell – Try It Online") Example usage: `f 10`.
[Answer]
# k, 33 bytes
```
`0:|" #"{(1+!x){~y!x}/:(x>)(2*)\1}
```
This only seems to work in [AW's interpreter](https://kparc.com).
[![Example of it working in AW's interpreter.](https://i.stack.imgur.com/ZKnb9.png)](https://i.stack.imgur.com/ZKnb9.png)
[The oK version](http://johnearnest.github.io/ok/?run=%600%3A%7C%22%20%23%22%7B%281%2B%21x%29%7B~y%21x%7D%2F%3A%7Bx%3Ey%7D%5Bx%5D%282%2a%29%5C1%7D32%3B) (which you can try online) seems to have a bug, requiring a slight change to make it work:
```
`0:|" #"{(1+!x){~y!x}/:{x>y}[x](2*)\1}
```
[Answer]
## C#, 174 bytes
This method has two parameters, an input for the ruler length, and an output which is the ruler as string.
Golfed:
```
void R(int n,out string s){var l=new int[++n];int i,x=n,y=0;for(s="";x-->1;)for(i=0;0==(l[x]=(x>>i++&1)*i);y=y<i?i:y);for(y++;y-->0;s+='\n')for(x=0;++x<n;s+=y<l[x]?'#':' ');}
```
Indented:
```
void R(int n,out string s){                       // Return the result in an out parameter.
    var l=new int[++n];                           // Use a 1-based array.
    int i,x=n,y=0;                                //
    for(s="";x-->1;)                              // For each number x on the ruler
        for(i=0;0==(l[x]=(x>>i++&1)*i);y=y<i?i:y) // ... find lowest set bit of x, counting the maximum value.
            ;                                     //
    for(y++;y-->0;s+='\n')                        // Count down each line.
        for(x=0;++x<n;s+=y<l[x]?'#':' ')          // Output # for numbers that are tall enough.
            ;                                     //
}
```
### [Try it online!](https://dotnetfiddle.net/inhmxA)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~27~~ 23 bytes
```
↶Ｆ…·¹Ｎ«Ｊι⁰#Ｗ¬﹪ι²«Ａ÷ι²ι#
```
[Try it online!](https://tio.run/##Vcy9DoIwFAXgGZ6iweU2gURxdDJhwSghxheopcBNSkv652B49irq4nhyznf4yAzXTMbYYtDuLHoH9JD22hCoFZfeYhBXpgYBu5zUavau8dNdGKCUkmeanPw03zRgTrZvl7QGlYNsk63hMaIUBBrt4KI7Lz@z8geTo7U4KKgwYCe@TU5wdf8vS7rEuC9jEWJh5Qs "Charcoal – Try It Online") Link is to verbose version of code. Edit: Saved 4 bytes by switching to `JumpTo`.
[Answer]
# J, 38 bytes
```
3 :'|.|:''#''#~,.(1+|.i.1:)@#:"0>:i.y'
```
Not great. Lmk if the byte count is off -- I'm on my phone.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 13 bytes
```
↔T' ↑moR'#→İr
```
[Try it online!](https://tio.run/##yygtzv7//1HblBB1hUdtE3Pzg9SVH7VNOrKh6P///8ZGAA "Husk – Try It Online")
I saw a ruler sequence builtin(`İr`), so I just went ahead and found a challenge I could cheat on.
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), 91 bytes
```
n->{int i=10,j;String s="";for(;i-->0;s+="\n")for(j=0;j++<n;)s+=j>>i<<i<j?' ':35;return s;}
```
[Try it online!](https://tio.run/##hY6xboNAEERr/BUrGoMwJxzHiuQD0kVKkcplkuKMAe0ZL@husWRZfDu5CPdX7Wjm7Wi0uqm0H2rS58s8jKcOK6g6ZS18KaTHKnh6lhW7c@vxDFeXREc2SO33LyjT2tiBgXZVYmTsRDNSxdiT@HiK/JO4bmuzgeWthAYKmCktH0gMWGyzjZZLBrYIQ9n0JpKYpmUmbVKEPxTG/5YuMqmTJCcZO1uXJeY55vp9DevDbi9NzaMhsHKag0C6Tce75foq@pHF4Mq5o6gRahi6e7SNYw/x4iV2XuLVS@y9xJt/xzJ1Wk3zHw "Java (OpenJDK 8) – Try It Online")
Ungolfed:
```
n->{
    int i=10,j; // Since we are allowed extra whitespace, set columns always to 10
    String s = "";
    for(;i-->0;s+="\n")      // Every iteration add a newline, i=9..0
        for(j=0;j++<n;)      // j=1..n+1
            s+= j>>i<<i<j    // if j has less than i trailing 0s in binary form
                ?' '         // add a space else
                :35          // # (java handles ternary return types weirdly)
}
```
[Answer]
## CJam, 34 bytes
```
ri{2bW%0+0#)}%_'#f*\:e>f{Se]}zW%N*
```
Meh.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 70 bytes
```
f(x,l,n){for(l=512;l/=2;)for(n=0;n++<=x;)putchar(n^x+1?n%l?32:35:10);}
```
[Try it online!](https://tio.run/##S9ZNT07@/z9No0InRydPszotv0gjx9bU0Mg6R9/WyFoTxM@zNbDO09a2sa2w1iwoLUnOSASKxVVoG9rnqebYGxtZGZtaGRpoWtf@z03MzNPQrOZSAII0DUMDoCCYDdRVrKGkBOWlaRgbAZm1/wE "C (gcc) – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/), 60 + 1 (-n) = 61 bytes
```
$n=0|1+(log)/log 2;say(($"x(2**$n-1),X)x($_/2**$n))while$n--
```
[Try it online!](https://tio.run/##K0gtyjH9/18lz9agxlBbIyc/XVMfSCgYWRcnVmpoqChVaBhpaank6Rpq6kRoVmioxOuD@Zqa5RmZOalACd3//83M/@UXlGTm5xX/1/U11TMwNPivmwcA "Perl 5 – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-R`](https://codegolf.meta.stackexchange.com/a/14339/), 11 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
õ_&n)¤çTÃz3
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LVI&code=9V8mbimk51TDejM&input=MzI)
```
õ_&n)¤çTÃz3     :Implicit input of integer U
õ               :Range [1,U]
 _              :Map each Z
  &n            :  Bitwise AND with -Z
    )           :  Group that operation
     ¤          :  Result to binary string
      ç         :  Fill with
       T        :    0
        Ã       :End map
         z3     :Rotate 90° clockwise 3 times
                :Implicit output joined with newlines
```
]