Untuk bilangan bulat positif apa pun k
, izinkan d(k)
menyatakan jumlah pembagi k
. Sebagai contoh, d(6)
adalah 4
, karena 6
memiliki 4
pembagi (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 k
adalah 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
:
#
#
# # #
# # # #
# # # # #
# # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # ## # # # # # # # # # # # # # # # # # # ## # ## # #
# # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # ## # # # # # # # # # # # # # # # # # # ## # ## # #
# # # # # # # # # # ## # # # # # # ## # # # # ## # # # # # # # ### # ## # # # # ## # # # # # # ## # # # ## # ### # # # ## # ### ### # # # # ### # ## # #
# # # # # # # # # # # ## # # # # # # ## # # # # ## # ## # # # # # ### # ## # # # # ## # # # # # # ## # # # ## # ### # # # ## # ### ### # # # # ### # ## # #
# # # # ### # ### # ### # ##### ### # ### # ### ##### # ##### ### # ##### ### ##### ####### ### # ### # ### ####### ##### ### ##### # ######### # ##### ##### ### # ### ##### # ######### # ### # #
# # ### # ### # ### ##### # ##### ### # ### ##### ##### # ##### ### # ##### ### ##### ####### ### # ### # ### ############# ### ##### # ######### # ##### ##### ### ##### ##### # ######### # ### # #
#######################################################################################################################################################################################################
########################################################################################################################################################################################################
C,
9995929190 bytesSee it work here.
sumber
Octave,
414032 bytesThanks to @StewieGriffin saved 8 bytes.
Try it online!
Previous answers:
Try it online!
Try it online!
Explanation:
sumber
@(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.@(N)['',35*sort(~mod(k=1:N,k'))]
.mod(1:N,(1:N).')
acceptable in MATLAB?Haskell, 71 bytes
f
takes anInt
and returns aString
.Try it online!
m
is theN
of the OP (Haskell variables must be lowercase.)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 ofn
.sumber
Octave, 61 bytes
Explanation:
A few things I wish to highlight
What happens inside the loop (suppose input
6
)1,2,4
)1,2,3,6
)Finally we flip it, and converts it to a string, implicitly changing the
0
to32
:sumber
Python 3, 111 bytes
Try it online!
This produces some leading vertical whitespace
sumber
APL (Dyalog), 19 bytes
Try it online!
⎕
get evaluated input (N)⍳
1...N∘.|⍨
division remainder table with 1...N both as vertical and as horizontal axis0=
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 downsumber
Mathematica,
5957 bytessumber
50
with#
and appending&
. You can save some bytes with infix notation as well:X~Table~#&
and0~DivisorSigma~Range@#
C#,
333281 BytesWith line breaks:
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
sumber
using
-statement.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 anAction
, move the increments around and a couple of other minor tweaks. I haven't tested that but it should work.Mathematica, 99 bytes
for N=50
sumber
(T=0~DivisorSigma~Range@#;Row[Column/@Table[Join[Table[,Max@T-T[[i]]],$~Table~T[[i]]],{i,1,#}]])&
Charcoal,
232220 bytesTry it online! Link is to verbose version of code. Edit: Saved 1 byte by looping
k
from0
toi-1
and adding1
inside the loop. Saved a further two bytes by not storing the input in a variable. Explanation: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!
sumber
05AB1E, 12 bytes
Code:
Explanation:
Uses the 05AB1E encoding. Try it online!
sumber
ζ
instead of.Bø
? Also, the character doesn't have to be#
ζ
didn't exist back then.Python 2, 101 bytes
Try it online!
Ini menghasilkan (banyak) spasi putih yang mengarah secara vertikal. Ini mencetak total
N
garis, sebagian besar yang biasanya akan kosong.sumber
Japt ,
34 33 1614 byteDisimpan 17 byte berkat produk @ETH
Cobalah online!
sumber
z
melakukan padding:õ_â lã'#pX÷z w
J , 28 byte
Mendefinisikan kata kerja monadik. Cobalah online!
Penjelasan
sumber
PHP, 126 Bytes
Cobalah online!
sumber
Brachylog , 34 byte
Cobalah online!
sumber
Alice, 33 bytes
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:
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 largerN
, 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 ast&w...k
to run a loop n times, witht
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
k
tidak 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:
Adalah loop bersarang yang berfungsi yang menjalankan
XYYYXYYYXYYY...
(untuk beberapa jumlahY
s 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.
sumber
Actually, 25 bytes
Try it online!
22-byte version with a lot of leading newlines
Try it online!
sumber
R,
8382 bytes-1 byte thanks to MickyT
Reads
N
from stdin.Try it online!
sumber
!=0
can be>0
Pyth, 16 bytes
Try it online!
sumber
SpecBAS - 149 bytes
An array keeps track of number of divisors, then prints the right number of characters down to screen position 50.
sumber
PHP, 99 bytes
prints one leading space; run as pipe with
php -nr '<code>'
or try it online.breakdown
sumber
PowerShell, 101 bytes
Less golfed test script:
Output:
sumber
SOGL V0.12, 8 bytes
Try it here!
sumber
Wolfram Language (Mathematica),
4644 bytesTry it online! But maybe try it online! with ColumnForm instead of Grid, since
Grid
doesn't work in TIO. In Mathematica, it looks better:A third Mathematica solution...
Divisors@Range@#
finds all the divisors in the range we want, and then we multiply by0
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 either0
or" "
.As an alternative, the 59-byte
""<>Riffle[PadLeft["X"-" "+0Divisors@Range@#]+" ","\n"]&
produces string output.sumber
Add++, 58 bytes
Try it online!
How it works
Our program consists of two helpers functions,x , between 1 and n and returns d(x) as defined in the challenge body, while
g
andk
, and the main lambda function.g
iterates over each integer,k
takes a list of characters and concatenates them into a single string.The main lambda function generatesA=[d(1),d(2),d(3),...,d(n)] , then repeats the 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.
#
character for each element ofsumber