Langit pembagi

46

Untuk bilangan bulat positif apa pun k, izinkan d(k)menyatakan jumlah pembagi k. Sebagai contoh, d(6)adalah 4, karena 6memiliki 4pembagi (yaitu 1, 2, 3, 6).

Diberikan bilangan bulat positif N, tampilkan "cakrawala" dalam seni ASCII menggunakan karakter tetap, sehingga ketinggian "bangunan" yang terletak pada posisi horizontal kadalah d(k)untuk k = 1, ..., N. Lihat uji kasus di bawah ini.

Aturan

  • Karakter non-spasi putih dapat digunakan secara konsisten, tidak harus #seperti yang ditunjukkan dalam kasus uji.
  • Algoritme secara teoritis harus bekerja untuk tinggi sewenang-wenang N. Dalam praktiknya, dapat diterima jika program dibatasi oleh waktu, memori, ukuran tipe data atau ukuran layar.
  • Ruang terdepan atau trailing horizontal atau vertikal diizinkan.
  • Input dan output dapat diambil dengan cara apa pun yang wajar .
  • Program atau fungsi diizinkan, dalam bahasa pemrograman apa pun . Celah standar dilarang.
  • Kode terpendek dalam byte menang.

Uji kasus

N = 10:

     # # #
   # # ###
 #########
##########

N = 50:

                                               #  
                                   #           #  
                       #     #     #   # #     #  
                       #     #     #   # #     #  
           #     # #   #   # # #   #   # # ##  # #
           #   # # #   #   # # #   #   # # ##  # #
     # # # # ### # ### # ### # ##### ### # ### # #
   # # ### # ### # ### ##### # ##### ### # ### ###
 #################################################
##################################################

N = 200:

                                                                                                                                                                                   #                    
                                                                                                                                                                                   #                    
                                                                                                                       #                                               #           #                    
                                                                                                                       #                       #                       #           #                    
                                                                                                                       #                       #                       #           #           #        
                                                                                                                       #                       #                       #           #           #        
                                                           #           #           #     #     #           #           #     #     #       #   #     #     #   #       #           #           #     # #
                                                           #           #           #     #     #           #           #     #     #       #   #     #     #   #       #           #           #     # #
                                               #           #           #       #   #     #     #           #   #       #     #     #       #   #     #     #   # #     #       #   #           #     # #
                                   #           #           #           #       #   #     #     #   #       #   #       #     #     #       #   #     #     #   # #     #       #   #           #   # # #
                       #     #     #   # #     #     # #   #     #   # #     # #   #   # #     #   # # ##  # # # #     #     # # # #  ## # #   #     # # # #   # #  #  # #   # #   # # # #  ## #  ## # #
                       #     #     #   # #     #     # #   #   # #   # #     # #   #   # #     #   # # ##  # # # #     #     # # # #  ## # #   #     # # # #   # #  #  # #   # #   # # # #  ## #  ## # #
           #     # #   #   # # #   #   # # ##  # # # # #   #  ## # # # #  ## # #   #   # # #   # ### # ##  # # # # ##  #   # # # # #  ## # #   #  ## # ### #   # # ##  # ### ###   # # # # ### #  ## # #
           #   # # #   #   # # #   #   # # ##  # # # # #   #  ## # # # #  ## # ##  #   # # #   # ### # ##  # # # # ##  #   # # # # #  ## # #   #  ## # ### #   # # ##  # ### ###   # # # # ### #  ## # #
     # # # # ### # ### # ### # ##### ### # ### # ### ##### # ##### ### # ##### ### ##### ####### ### # ### # ### ####### ##### ### ##### # ######### # ##### ##### ### # ### ##### # ######### # ### # #
   # # ### # ### # ### ##### # ##### ### # ### ##### ##### # ##### ### # ##### ### ##### ####### ### # ### # ### ############# ### ##### # ######### # ##### ##### ### ##### ##### # ######### # ### # #
 #######################################################################################################################################################################################################
########################################################################################################################################################################################################
Luis Mendo
sumber

Jawaban:

12

Jelly , 9 byte

Gunakan 0bukan #.

RÆD0ṁz⁶ṚY

Cobalah online!

Biarawati Bocor
sumber
2
5 menit 10 detik :-)
Luis Mendo
13
Saya sedang membaca hal-hal lain sementara itu ... Saya akan lebih cepat lain kali.
Leaky Nun
7

C, 99 95 92 91 90 bytes

c,l,i,j;f(n){for(j=n;j--;puts(""))for(i=0;i<n;c=!putchar(32|c>j))for(l=++i;l;c+=i%l--<1);}

See it work here.

2501
sumber
7

Octave, 41 40 32 bytes

Thanks to @StewieGriffin saved 8 bytes.

@(N)" #"(sort(~mod(k=1:N,k'))+1)

Try it online!

Previous answers:

@(N)" #"(sort(~bsxfun(@mod,k=1:N,k')+1))

Try it online!

@(N)" #"(sort(ismember((k=1:N)./k',k))+1)

Try it online!

Explanation:

N=5;
d = (1:N)./(1:N)'    %divide each of numbers from 1 to N by 1 to N 
                     %a [N by N] matrix created
d =

   1.00   2.00   3.00   4.00   5.00
   0.50   1.00   1.50   2.00   2.50
   0.33   0.66   1.00   1.33   1.66
   0.25   0.50   0.75   1.00   1.25
   0.20   0.40   0.60   0.80   1.00

m = ismember(d,1:N)      %find divisors of each number
m =

  1  1  1  1  1
  0  1  0  1  0
  0  0  1  0  0
  0  0  0  1  0
  0  0  0  0  1

idx = sort(m)                  %sort the matrix

idx =

  0  0  0  0  0
  0  0  0  0  0
  0  0  0  1  0
  0  1  1  1  1
  1  1  1  1  1

" #"(idx+1)     %replace 0 and 1 with ' ' and '#' respectively


   #
 ####
#####
rahnema1
sumber
1
Very nice approach!
Luis Mendo
2
Octave performs singleton expansion implicitly, so @(N)" #"(sort(~mod(k=1:N,k')+1)) saves you a few bytes :) You get a bunch of leading newlines though, I'm not sure what the rules are in that regard.
Stewie Griffin
1
Same bytecount (32): @(N)['',35*sort(~mod(k=1:N,k'))].
Stewie Griffin
@StewieGriffin Thanks! I didn't aware of that feature . Is mod(1:N,(1:N).') acceptable in MATLAB?
rahnema1
2
I think it is possible as of R2016b, but I can't test it myself unfortunately, since I don't have it.
Stewie Griffin
6

Haskell, 71 bytes

f takes an Int and returns a String.

f m|l<-[1..m]=unlines[[last$' ':drop(m-i)['#'|0<-mod n<$>l]|n<-l]|i<-l]

Try it online!

  • m is the N of the OP (Haskell variables must be lowercase.)
  • The abbreviation l=[1..m] is used in the nested list comprehensions for iterating through all of rows, columns, and potential divisors. This means some extra initial rows filled with whitespace.
  • n is column (also number checked), i is row.
  • ['#'|0<-mod n<$>l] is a list of '#' characters with length the number of divisors of n.
Ørjan Johansen
sumber
6

Octave, 61 bytes

for i=1:input(''),p(1:nnz(~rem(i,1:i)),i)=35;end,[flip(p),'']

Explanation:

for i=1:input('')         % Loop, for i from 1 to the input value
 p(1:nnz(~rem(i,1:i)),i)=35;end,[flip(p),'']   
% Breakdown:
         ~rem(i,1:i)      % Return true if the remainder of i divided by any of the values
                          % in the vector 1 - i
     nnz(~rem(i,1:i))     % Check how many of them are non-zero
 p(1:nnz(~rem(i,1:i)),i)=35;end   % Assign the value 35 (ASCII for #) to rows 1
                                  % to the number of divisors of i, in column i
end,              % End loop
[flip(p),'']      % Flip the matrix, and concatenate it with the empty string

A few things I wish to highlight

  • Takes the input directly into the loop
    • Doesn't assign the input value to any variable
  • Doesn't initialize any array
    • It creates it on the fly, adding columns and rows as necessary
  • Automatically casts ASCII-value 0 to whitespace (ASCII-32)

What happens inside the loop (suppose input 6)

p =  35
p =
   35   35
    0   35
p =
   35   35   35
    0   35   35
p =
   35   35   35   35
    0   35   35   35
    0    0    0   35
p =
   35   35   35   35   35
    0   35   35   35   35
    0    0    0   35    0
p =
   35   35   35   35   35   35
    0   35   35   35   35   35
    0    0    0   35    0   35
    0    0    0    0    0   35

  1. Starts of as a single 35
  2. Expands one column and one row, to make room for the two divisors of 2
  3. Expands one column, to make room for 3 (only two divisors)
  4. Expands one column and one row, to make room for the 3 divisors (1,2,4)
  5. Expands one column, to make room for 5
  6. Expands one column and one row, to make room for the 4 divisors (1,2,3,6)

Finally we flip it, and converts it to a string, implicitly changing the 0 to 32:

warning: implicit conversion from numeric to char
ans =
     #
   # #
 #####
######
Stewie Griffin
sumber
5

Python 3, 111 bytes

lambda n:'\n'.join([*map(''.join,zip(*['#'*sum(x%-~i==0for i in range(x))+n*' 'for x in range(1,n+1)]))][::-1])

Try it online!


This produces some leading vertical whitespace

ovs
sumber
5

APL (Dyalog), 19 bytes

⊖⍉↑'#'⍴¨⍨+⌿0=∘.|⍨⍳⎕

Try it online!

 get evaluated input (N)

 1...N

∘.|⍨ division remainder table with 1...N both as vertical and as horizontal axis

0= where equal to zero (i.e. it divides)

+⌿ sum the columns (i.e. gives count of divisors for each number)

'#'⍴¨⍨ use each number to reshape the hash character (gives list of strings)

 mix (list of strings into table of rows)

 transpose

 flip upside down

Adám
sumber
5

Mathematica, 59 57 bytes

Rotate[Grid@Map[X~Table~#&,0~DivisorSigma~Range@#],Pi/2]&

masukkan deskripsi gambar di sini

Ian Miller
sumber
Welcome to answering on PPCG, fellow Lego minifig :-)
Luis Mendo
1
Now there's no turning back...
Luis Mendo
Welcome! Nice to see another Mathematica golfer. This answer isn't entirely valid though because you've hardcoded the input into the snippet. Answers need to be full programs or callable functions (which may be unnamed though). So this can be fixed at no cost by replacing 50 with # and appending &. You can save some bytes with infix notation as well: X~Table~#& and 0~DivisorSigma~Range@#
Martin Ender
@MartinEnder Thanks. I forgot that bit when I moved from testing to answering. And thanks for the hint about infixing. Its not something I normally use (as I don't really golf).
Ian Miller
I thought as much. It was more a tongue in cheek comment. Sorry for the confusion.
Ian Miller
5

C#, 333 281 Bytes

using System.Linq;using C=System.Console;class P{static void Main(){var n=int.Parse(C.ReadLine());var v=new int[n];for(var k=2;k<=n;k++)for(var i=1;i<k;i++)if(k%i==0)v[k-1]++;for(var i=0;i<v.Length;i++)for(var u=v.Max()-v[i];u<v.Max();u++){C.SetCursorPosition(i,u);C.Write("#");}}}

With line breaks:

using System.Linq;
using C = System.Console;

class P
{
    static void Main()
    {
        var n = int.Parse(C.ReadLine());
        var v = new int[n];
        for (var k = 2; k <= n; k++)
            for (var i = 1; i < k; i++)
                if (k % i == 0)
                    v[k - 1]++;
        for (var i = 0; i < v.Length; i++)
            for (var u = v.Max() - v[i]; u < v.Max(); u++)
            {
                C.SetCursorPosition(i, u);
                C.Write("#");
            }
    }
}

While I'm sure this is possible shorter as well, I hope we'll achieve a shorter solution together ;)

Saved 52 Bytes with the help of raznagul

MetaColon
sumber
1
If you use an int-array instead of a list, you can save a lot of bytes from the using-statement.
raznagul
@raznagul updated it.
MetaColon
222 bytes: using System.Linq;using C=System.Console;n=>{var v=new int[n];for(var k=1,i;++k<=n;)for(i=1;i<k;)if(k%i++==0)v[k-1]++;for(var i=0,u;i<v.Length;i++)for(u=v.Max()-v[i];u<v.Max();){C.SetCursorPosition(i, u++);C.Write("#");}}; Compile to an Action, move the increments around and a couple of other minor tweaks. I haven't tested that but it should work.
TheLethalCoder
@TheLethalCoder I'll test it / update my answer tomorrow.
MetaColon
5

Mathematica, 99 bytes

{T=DivisorSigma[0,Range@#];Row[Column/@Table[Join[Table[,Max[T]-T[[i]]],$~Table~T[[i]]],{i,1,#}]]}&

for N=50

enter image description here

J42161217
sumber
Are all those spaces (and newlines) necessary for the code to run? I've never programming Mathematica myself, but in most languages you could remove almost all of those spaces.
Kevin Cruijssen
this is my first golf. thanks for the tips
J42161217
2
No problem, and welcome to PPCG! If you haven't yet, you might find Tips for golfing in <all languages> and Tips for golfing in Mathematica interesting to read through. :) Enjoy your stay.
Kevin Cruijssen
The outer curly braces can be changed to round brackets to prevent them from showing in the output, and you can use some infix/prefix syntax to save 2 bytes: (T=0~DivisorSigma~Range@#;Row[Column/@Table[Join[Table[,Max@T-T[[i]]],$~Table~T[[i]]],{i,1,#}]])&
numbermaniac
5

Charcoal, 23 22 20 bytes

F…·¹N«Jι⁰Fι¿¬﹪ι⁺κ¹↑#

Try it online! Link is to verbose version of code. Edit: Saved 1 byte by looping k from 0 to i-1 and adding 1 inside the loop. Saved a further two bytes by not storing the input in a variable. Explanation:

F…·¹N       for (i : InclusiveRange(1, InputNumber()))
«           {
 Jι⁰         JumpTo(i, 0);
 Fι          for (k : Range(i))
  ¿¬﹪ι⁺κ¹     if (!(i % (k + 1)))
   ↑#          Print(:Up, "#");
            }

Edit: This 18-byte "one-liner" (link is to verbose version of code) wouldn't have worked with the version of Charcoal at the time the question was submitted: Try it online!

↑E…·¹N⪫Eι⎇﹪ι⁺¹λω#ω
Neil
sumber
better explanation :P
ASCII-only
3

05AB1E, 12 bytes

Code:

LÑ€g'#×.BøR»

Explanation:

L             # Create the range 1 .. input
 Ñ            # Get the list of divisors of each
  €g          # Get the length of each element
    '#        # Push a hash tag character
      ×       # String multiply
       .B     # Squarify, make them all of equal length by adding spaces
         ø    # Transpose
          R   # Reverse the array
           »  # Join by newlines

Uses the 05AB1E encoding. Try it online!

Adnan
sumber
Can't you use ζ instead of .Bø? Also, the character doesn't have to be #
Oliver Ni
ζ didn't exist back then.
Magic Octopus Urn
3

Python 2, 101 bytes

N=input();r=range
for i in r(N,0,-1):print''.join('# '[i>sum(x%-~p<1for p in r(x))]for x in r(1,1+N))

Try it online!

Ini menghasilkan (banyak) spasi putih yang mengarah secara vertikal. Ini mencetak total Ngaris, sebagian besar yang biasanya akan kosong.

mathmandan
sumber
3

Japt , 34 33 16 14 byte

Disimpan 17 byte berkat produk @ETH

õ@'#pXâ l÷z w

Cobalah online!

Luke
sumber
Anda dapat menyimpan banyak byte hanya dengan membiarkan zmelakukan padding:õ_â lã'#pX÷z w
ETHproduksi
Tidak tahu bahwa melapisi string secara otomatis. Terima kasih!
Lukas
2

J , 28 byte

[:|:&.|.[('#',@$~1+_&q:)@-i.

Mendefinisikan kata kerja monadik. Cobalah online!

Penjelasan

[:|:&.|.[('#',@$~1+_&q:)@-i.  Input is y.
                          i.  Range from 0 to y-1
        [                -    subtracted from y (gives range from y to 1).
         (             )@     For each of the numbers:
                   _&q:         Compute exponents in prime decomposition,
                 1+             add 1 to each,
          '#'  $~               make char matrix of #s with those dimensions,
             ,@                 and flatten into a string.
                              The resulting 2D char matrix is padded with spaces.
[:|:&.|.                      Reverse, transpose, reverse again.
Zgarb
sumber
2

PHP, 126 Bytes

for(;$n++<$argn;)for($c=$d=0;$d++<$n;)$n%$d?:$r[$n]=++$c;for($h=max($r);$h--;print"
")for($n=0;$n++<$argn;)echo$h<$r[$n]?:" ";

Cobalah online!

Jörg Hülsermann
sumber
2

Alice, 33 bytes

I.!.t&w?t!aot&wh.Bdt.?-ex' +o&;k@

Try it online!

Input is (unfortunately) in the form of a code point. At least it reads a UTF-8 character, so you can use larger inputs than 255, but they're still limited and it's a pretty painful input format. For three additional bytes, we can read a decimal integer:

/
ki@/.!.t&w?t!aot&wh.Bdt.?-ex' +o&;

Try it online!

The non-whitespace character in the output is !.

Note that the solution also prints a ton of leading whitespace (it always starts with an empty line and then prints an NxN grid so for larger N, there will be many lines of spaces before the first !s.)

Explanation

I've used and explained the &w...k construction before (for example here). It's a neat little idiom that pops an integer n and then runs a piece of code n+1 times (consequently, it's usually used as t&w...k to run a loop n times, with t decrementing the input value). This is done by working with the return address stack (RAS). w pushes the current IP address to the RAS, and if we repeat it with & then the address gets pushed n times. k pops one address from the RAS and jumps back there. If the RAS is empty, it does nothing at all and the loop is exited.

Anda mungkin memperhatikan bahwa itu tidak mungkin untuk membuat simpai loop ini, karena pada akhir loop batin, stack tidak kosong, sehingga ktidak menjadi no-op. Sebaliknya, IP akan melompat kembali ke awal lingkaran luar. Cara umum untuk memperbaikinya adalah membungkus lingkaran dalam dengan subrutinnya sendiri. Tetapi jika kita dapat mengatur loop bersarang sehingga loop luar berakhir dengan loop dalam, kita benar-benar dapat memanfaatkan perilaku ini dan bahkan menghemat satuk!

Jadi konstruksi ini:

&wX&wYk

Adalah loop bersarang yang berfungsi yang menjalankan XYYYXYYYXYYY...(untuk beberapa jumlah Ys di setiap iterasi). Cukup rapi bahwa kita dapat mengakhiri kedua loop dengan satuk, because it will consume an outer address from RAS each time the inner addresses have been depleted.

Idiom ini digunakan dalam program untuk menjalankan loop di atas grid output.

I         Read a character and push its code point as input N.
.!        Store a copy on the tape.
.         Make another copy.
t&w       Run this outer loop N times. This loops over the lines of the
          output, so the current iteration corresponds to a divisor count
          no more than i (i counting down from N).
  ?t!       Decrement the value on the tape. That means we'll actually have
            i-1 on the tape during the iteration.
  ao        Print a linefeed. Doing this now leads to the weird leading linefeed, 
            but it's necessary for the &w...&w...k pattern.
  t&w       Remember that we still have a copy of N on the stack. We'll also
            ensure that this is the case after each loop iteration. So this
            also runs the inner loop N times. This iterates over the columns
            of the output grid so it's j that goes from 1 to N and corresponds
            to the number whose divisors we want to visualise.
              At this point the stack will always be empty. However, operating
              on an empty stack works with implicit zeros at the bottom of
              the stack. We'll use the top zero to keep track of j.
    h         Increment j.
    .B        Duplicate j and push all of its divisors.
    dt        Push the stack depth minus 1, i.e. the divisor count of j.
    .         Duplicate the divisor count.
    ?-        Retrieve i-1 from the tape and subtract it. So we're computing
              d(j)-i+1. We want to output a non-whitespace character iff this
              value is positive (i.e. if i is no greater than d(j)).
    ex        Extract the most significant bit of this value. This is 1 for
              all positive values and 0 for non-positive ones. It's the shortest
              way (I believe) to determine if a value is positive.
    ' +       Add this to the code point of a space. This gives a 33, or '!',
              if the cell is part of the skyline.
    o         Output the character.
    &;        We still have all the divisors and the divisor count on the stack.
              We pop the divisor count to discard that many values (i.e. to
              get rid of the divisors again).
k         Return to the appropriate loop beginning to run the continue the
          nested loop.
@         Terminate the program.
Martin Ender
sumber
1
First Alice program with a single line ever? :-)
Luis Mendo
1
@LuisMendo No, I think Leo has written a few Cardinal-only programs (and maybe I have as well... the quine and the Hello, World for example). Probably the most elaborate single-line program though. :)
Martin Ender
Hmm, my solution could save some bytes with "ton of leading whitespace" trick
quintopia
2

Actually, 25 bytes

R♂÷♂l;M' *╗⌠'#*╜@+⌡M┬♂ΣRi

Try it online!

22-byte version with a lot of leading newlines

;╗R⌠÷l'#*╜' *@+⌡M┬♂ΣRi

Try it online!

Leaky Nun
sumber
Okay, the Readme on the github should be updated to include ÷l as the standard divisor counting function...
quintopia
2

R, 83 82 bytes

-1 byte thanks to MickyT

N=scan();m=matrix('#',N,N);for(i in 1:N)m[i,1:sum(i%%1:N>0)]=' ';write(m,'',N,,'')

Reads N from stdin.

N=scan()                        # read N
m=matrix('#',N,N)               # make an NxN matrix of '#' characters
for(i in 1:N)                   # for each integer in the range
    m[i,1:sum(i%%1:N!=0)]=' '   # set the first n-d(n) columns of row i to ' '
write(m,'',N,,'')               # print to console with appropriate formatting

Try it online!

Giuseppe
sumber
1
!=0 can be >0
MickyT
1

SpecBAS - 149 bytes

1 INPUT n: DIM h(n)
2 FOR i=1 TO n
3 FOR j=1 TO i
4 h(i)+=(i MOD j=0)
5 NEXT j
6 NEXT i
7 FOR i=1 TO n
8 FOR j=51-h(i) TO 50
9  ?AT j,i;"#"
10 NEXT j
11 NEXT i

An array keeps track of number of divisors, then prints the right number of characters down to screen position 50.

enter image description here

Brian
sumber
1

PHP, 99 bytes

for(;$d<$k?:$k++<$argn+$d=$n=0;)$k%++$d?:$r[--$n]=str_pad($r[$n],$k).H;ksort($r);echo join("
",$r);

prints one leading space; run as pipe with php -nr '<code>' or try it online.

breakdown

for(;$d<$k?:                    # inner: loop $d from 1 to $k
    $k++<$argn+$d=$n=0;)        # outer: loop $k from 1 to $argn, reset $d and $n
    $k%++$d?:                       # if $d divides $k
        $r[--$n]=str_pad($r[$n],$k).H;  # add one store to building $k
ksort($r);echo join("\n",$r);   # reverse and print resulting array
Titus
sumber
1

PowerShell, 101 bytes

((($d=1.."$args"|%{($x=$_)-(1..$_|?{$x%$_}).Count})|sort)[-1])..1|%{$l=$_;-join($d|%{' #'[$_-ge$l]})}

Less golfed test script:

$f = {

$d=1.."$args"|%{
    ($x=$_)-(1..$_|?{$x%$_}).Count
}                                     # $d is the Divisor count array
$maxHeight=($d|sort)[-1]
$maxHeight..1|%{
    $l=$_
    -join($d|%{' #'[$_-ge$l]})
}

}

@(
    ,(10, 
    "     # # #",
    "   # # ###",
    " #########",
    "##########")

    ,(50, 
    "                                               #  ",
    "                                   #           #  ",
    "                       #     #     #   # #     #  ",
    "                       #     #     #   # #     #  ",
    "           #     # #   #   # # #   #   # # ##  # #",
    "           #   # # #   #   # # #   #   # # ##  # #",
    "     # # # # ### # ### # ### # ##### ### # ### # #",
    "   # # ### # ### # ### ##### # ##### ### # ### ###",
    " #################################################",
    "##################################################")

    ,(200,
    "                                                                                                                                                                                   #                    ",
    "                                                                                                                                                                                   #                    ",
    "                                                                                                                       #                                               #           #                    ",
    "                                                                                                                       #                       #                       #           #                    ",
    "                                                                                                                       #                       #                       #           #           #        ",
    "                                                                                                                       #                       #                       #           #           #        ",
    "                                                           #           #           #     #     #           #           #     #     #       #   #     #     #   #       #           #           #     # #",
    "                                                           #           #           #     #     #           #           #     #     #       #   #     #     #   #       #           #           #     # #",
    "                                               #           #           #       #   #     #     #           #   #       #     #     #       #   #     #     #   # #     #       #   #           #     # #",
    "                                   #           #           #           #       #   #     #     #   #       #   #       #     #     #       #   #     #     #   # #     #       #   #           #   # # #",
    "                       #     #     #   # #     #     # #   #     #   # #     # #   #   # #     #   # # ##  # # # #     #     # # # #  ## # #   #     # # # #   # #  #  # #   # #   # # # #  ## #  ## # #",
    "                       #     #     #   # #     #     # #   #   # #   # #     # #   #   # #     #   # # ##  # # # #     #     # # # #  ## # #   #     # # # #   # #  #  # #   # #   # # # #  ## #  ## # #",
    "           #     # #   #   # # #   #   # # ##  # # # # #   #  ## # # # #  ## # #   #   # # #   # ### # ##  # # # # ##  #   # # # # #  ## # #   #  ## # ### #   # # ##  # ### ###   # # # # ### #  ## # #",
    "           #   # # #   #   # # #   #   # # ##  # # # # #   #  ## # # # #  ## # ##  #   # # #   # ### # ##  # # # # ##  #   # # # # #  ## # #   #  ## # ### #   # # ##  # ### ###   # # # # ### #  ## # #",
    "     # # # # ### # ### # ### # ##### ### # ### # ### ##### # ##### ### # ##### ### ##### ####### ### # ### # ### ####### ##### ### ##### # ######### # ##### ##### ### # ### ##### # ######### # ### # #",
    "   # # ### # ### # ### ##### # ##### ### # ### ##### ##### # ##### ### # ##### ### ##### ####### ### # ### # ### ############# ### ##### # ######### # ##### ##### ### ##### ##### # ######### # ### # #",
    " #######################################################################################################################################################################################################",
    "########################################################################################################################################################################################################")


) | % {
    $n,$expected = $_
    $result = &$f $n
    "$result"-eq"$expected"
    #$result    # uncomment this to enjoy the view of the Divisor skyline
}

Output:

True
True
True
mazzy
sumber
1

Wolfram Language (Mathematica), 46 44 bytes

Grid[PadLeft[0Divisors@Range@#-" "]+" "]&

Try it online! But maybe try it online! with ColumnForm instead of Grid, since Grid doesn't work in TIO. In Mathematica, it looks better:

Mathematica output

A third Mathematica solution... Divisors@Range@# finds all the divisors in the range we want, and then we multiply by 0 and subtract " ", making every divisor equal to -" ".

PadLeft adds zeroes on the left, creating a sideways skyline, whose orientation we fix with =\[Transpose]. Finally, adding " " to everything makes all entries either 0 or " ".

As an alternative, the 59-byte ""<>Riffle[PadLeft["X"-" "+0Divisors@Range@#]+" ","\n"]& produces string output.

Misha Lavrov
sumber
1

Add++, 58 bytes

D,g,@,FL1+
D,k,@~,J
L,R€gd"#"€*$dbM€_32C€*z£+bUBcB]bR€kbUn

Try it online!

How it works

Our program consists of two helpers functions, g and k, and the main lambda function. g iterates over each integer, x, between 1 and n and returns d(x) as defined in the challenge body, while k takes a list of characters and concatenates them into a single string.

The main lambda function generates A=[d(1),d(2),d(3),...,d(n)], then repeats the # character for each element of A. We then take max(A), and subtract each element in A from this maximum, before yielding this number of spaces for each element and concatenating the spaces to the repeated hashes. Next, we transpose and reverse the rows, before joining each line on newlines. Finally, we output the skyline.

caird coinheringaahing
sumber