Skip to main content
added 738 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Jelly, 8  8  7 bytes

sÆDŒHḢṪ¬sⱮ½¬EƇṪ

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros as near to square as possible (more rows than columns if non-square \$N\$). (Won't work for huge inputs due to floating point inaccuracies creeping in.)

Try it online!

How?

sⱮ½¬EƇṪ - Link: positive integer, N
  ½     - square root {N}
 Ɱ      - map across {x in [1 .. floor(root(N))]} with:
s       -   split {[1..N]} into chunks of length {x} (trailing shorter if need be)
   ¬    - logical NOT (converts all values to zeros)
     Ƈ  - keep those for which:
    E   -   all equal?
      Ṫ - tail

8 bytes

(With no floating point inaccuracy)

Either change ½ to ƽ in the above or...

sÆDŒHḢṪ¬

Try it online!

How?

sÆDŒHḢṪ¬ - Link: positive integer, N
 ÆD      - divisors of {N}
s        - {[1..N]} split into chunks of {those}
   ŒH    - split into two (with first half one longer when N is square)
     Ḣ   - head of {that} -> all ways to make a rectangle with rows <= root(N)
      Ṫ  - tail of {that} -> the one with the most rows
       ¬ - logical NOT (convert all the values to zeros)

Jelly, 8 bytes

sÆDŒHḢṪ¬

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros as near to square as possible (more rows than columns if non-square \$N\$).

Try it online!

How?

sÆDŒHḢṪ¬ - Link: positive integer, N
 ÆD      - divisors of {N}
s        - {[1..N]} split into chunks of {those}
   ŒH    - split into two (with first half one longer when N is square)
     Ḣ   - head of {that} -> all ways to make a rectangle with rows <= root(N)
      Ṫ  - tail of {that} -> the one with the most rows
       ¬ - logical NOT (convert all the values to zeros)

Jelly,  8  7 bytes

sⱮ½¬EƇṪ

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros as near to square as possible (more rows than columns if non-square \$N\$). (Won't work for huge inputs due to floating point inaccuracies creeping in.)

Try it online!

How?

sⱮ½¬EƇṪ - Link: positive integer, N
  ½     - square root {N}
 Ɱ      - map across {x in [1 .. floor(root(N))]} with:
s       -   split {[1..N]} into chunks of length {x} (trailing shorter if need be)
   ¬    - logical NOT (converts all values to zeros)
     Ƈ  - keep those for which:
    E   -   all equal?
      Ṫ - tail

8 bytes

(With no floating point inaccuracy)

Either change ½ to ƽ in the above or...

sÆDŒHḢṪ¬

Try it online!

How?

sÆDŒHḢṪ¬ - Link: positive integer, N
 ÆD      - divisors of {N}
s        - {[1..N]} split into chunks of {those}
   ŒH    - split into two (with first half one longer when N is square)
     Ḣ   - head of {that} -> all ways to make a rectangle with rows <= root(N)
      Ṫ  - tail of {that} -> the one with the most rows
       ¬ - logical NOT (convert all the values to zeros)
Post Undeleted by Jonathan Allan
added 110 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Jelly, 48 bytes

sƽ¬sÆDŒHḢṪ¬

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros with a length just greater than \$\sqrt{N}\$ with \$N\$ total elementsas near to square as possible (each inner list having a length just lessmore rows than columns if non-square \$\sqrt{N}\$\$N\$).

Try it online!Try it online!

How?

sƽ¬sÆDŒHḢṪ¬ - Link: positive integer, N 
 ÆD      - divisors of {N}
s   e.g.  8
 ƽ  - integer square root {[1..N]} split into chunks of {those}
   ŒH    - split into two (with first half one longer when N is 2square)
s    - split {[1..N]} into chunks- head of {that} size-> all ways [[1,2],[3,4],[5,6],[7,8]]
to make a ¬rectangle -with logicalrows NOT<= root(N)
        - tail of {that} -> the one with the most rows
       ¬ - logical NOT (convert all the [[0,0],[0,0],[0,0],[0,0]]values to zeros)

Jelly, 4 bytes

sƽ¬

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros with a length just greater than \$\sqrt{N}\$ with \$N\$ total elements (each inner list having a length just less than \$\sqrt{N}\$).

Try it online!

How?

sƽ¬ - Link: positive integer, N             e.g.  8
 ƽ  - integer square root                         2
s    - split {[1..N]} into chunks of {that} size   [[1,2],[3,4],[5,6],[7,8]]
   ¬ - logical NOT                                 [[0,0],[0,0],[0,0],[0,0]]

Jelly, 8 bytes

sÆDŒHḢṪ¬

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros as near to square as possible (more rows than columns if non-square \$N\$).

Try it online!

How?

sÆDŒHḢṪ¬ - Link: positive integer, N 
 ÆD      - divisors of {N}
s        - {[1..N]} split into chunks of {those}
   ŒH    - split into two (with first half one longer when N is square)
        - head of {that} -> all ways to make a rectangle with rows <= root(N)
        - tail of {that} -> the one with the most rows
       ¬ - logical NOT (convert all the values to zeros)
Post Deleted by Jonathan Allan
deleted 24 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Jelly, 4 bytes

sƽ¬

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros with a length just greater than \$\sqrt{N}\$ with \$N\$ total elements (each inner list having a length just less than \$\sqrt{N}\$).

Try it online!

How?

sƽ¬ - Link: positive integer, N             e.g. 24 8
 ƽ  - integer square root                         42
s    - split {[1..N]} into chunks of {that} size   [[1,22],3[3,4],[5,6,7,8],...,[21,226],23[7,24]]8]]
   ¬ - logical NOT                                 [[0,0,0,0],[0,0,0,0],...,[0,00],0[0,0]]

Jelly, 4 bytes

sƽ¬

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros with a length just greater than \$\sqrt{N}\$ with \$N\$ total elements (each inner list having a length just less than \$\sqrt{N}\$).

Try it online!

How?

sƽ¬ - Link: positive integer, N             e.g. 24
 ƽ  - integer square root                         4
s    - split {[1..N]} into chunks of {that} size   [[1,2,3,4],[5,6,7,8],...,[21,22,23,24]]
   ¬ - logical NOT                                 [[0,0,0,0],[0,0,0,0],...,[0,0,0,0]]

Jelly, 4 bytes

sƽ¬

A monadic Link that accepts a positive integer, \$N\$, and yields a rectangular list of lists of zeros with a length just greater than \$\sqrt{N}\$ with \$N\$ total elements (each inner list having a length just less than \$\sqrt{N}\$).

Try it online!

How?

sƽ¬ - Link: positive integer, N             e.g.  8
 ƽ  - integer square root                         2
s    - split {[1..N]} into chunks of {that} size   [[1,2],[3,4],[5,6],[7,8]]
   ¬ - logical NOT                                 [[0,0],[0,0],[0,0],[0,0]]
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282
Loading