11
\$\begingroup\$

ISO Paper Sizes Defined:

The A series paper sizes are defined by the following requirements:

The length divided by the width is the square root of 2.
The A0 size has an area of 1 unit.
Each subsequent size A(n) is defined as A(n-1) cut in half parallel to its shorter sides.

enter image description here

Task:

given an input f[n] output A0 divided into n subdivisions.

Test cases:

f[1] to f[12]:

enter image description here

Here, A0 is given by f[1], but this indexing isn't essential.

Winning criteria:

The shortest code in bytes wins.

\$\endgroup\$
  • 1
    \$\begingroup\$ Shouldn't f[2] be an A0 with a horizontal line dividing it? i.e., isn't the f[2] in the test case actually f[3]? \$\endgroup\$ – JungHwan Min Dec 18 '16 at 1:35
  • \$\begingroup\$ @JungHwanMin adjusted now \$\endgroup\$ – martin Dec 18 '16 at 2:15
2
\$\begingroup\$

BBC BASIC 49 ASCII characters

Tokenised filesize 44 bytes

I.n:F.i=0TOn:RECTANGLE0,0,1189>>i/2+.5,841>>i/2N.

Much shorter than before! I always forget about the bitshift operators in BBC BASIC for windows as they weren't available on my old computer back in the day.

BBC BASIC 63 ASCII characters

Tokenised filesize 58 bytes

Dowload interpreter at http://www.bbcbasic.co.uk/bbcwin/download.html

A%=841C%=1189d=4I.n:F.i=0TOn:RECTANGLE0,0,C%,A%:d!^B%/=2d=-d:N.

Uses zero indexing, which I prefer. Thus 0 outputs the paper for A0, 1 outputs A0 divided into a pair of A1s, etc.

It is necessary to alternate between halving the X and Y coordinates, but doing that in an array would have cost too many bytes. Instead I use the fact that BBC basic has a block of static integer variables A%..Z% of 4 bytes each stored in contiguous memory. I store the X and Y values in A% and C% and access using the pointer to %B modified by the value of d, which alternates between 4 and -4.

Ungolfed

  A%=841
  C%=1189
  d=4
  INPUTn
  FORi=0TOn
    RECTANGLE0,0,C%,A%
    d!^B%/=2
    d=-d
  NEXT

Output

enter image description here

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6) + HTML, 96 94 + 34 = 130 128 bytes

f=(n,w=297,h=210)=>n--&&f(n,h<w?w/2:w,h<w?h:h/2,(C=c.getContext`2d`).rect(0,0,w,h),C.stroke())

f(8)
<canvas id=c width=300 height=300>

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 87 85 bytes

Thanks @martin for 1 byte.

Graphics@{EdgeForm@Thin,White,Rectangle[#,0{,}]&/@NestList[Sort[#/a]&,{1,a=√2},#]}&
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Very nice! EdgeForm@Thin: -1 byte \$\endgroup\$ – martin Dec 18 '16 at 2:21
2
\$\begingroup\$

JavaScript (ES6)/SVG (HTML5), 170 bytes

a=prompt();document.write('<svg width=297 height=210>');for(w=297,h=210;a--;h>w?h/=2:w/=2)document.write(`<rect fill=none stroke=#000 x=0 y=0 width=${w} height=${h} />`);

Uses 1-based indexing.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.