0
\$\begingroup\$

So a colleaque came with this problem lately and I want to challange you to solve the problem in the code-golf style, shortest solution in any language wins.

The Problem:

Given is a random set of values, where the values themselfs are not the concern, but the amount (which is random as well)of values.

To keep the posts readable, let's limit the amount of values to 200, but the program must be able to handly any amount of values, which the language supports.

Input: No Input needet, due the programm should set an random amount of values (limited to 200).

Output: Can be text or graphic(px) I recommend to use single digits as values if you use text based output, to keep the output clear.

Example:

{1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7}   -- Amount : 16

Now you should sort these values in a approcimated square matrix like:

1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
(4 x 4)

Pretty simple so far, if not boring.

But now the challange:

The number of fields in the matrix must be equivalent to the amount of values given. No empty fields or ignored values are allowed.

As an example, with 12 values, an square matrix is not possible, so you have to find the next aproximitation to it, which is 3x4 or 4x3 but NOT 4x4.

For any prime, the matrix would be 1 x prime

Examples:

For 12 Values:

1 2 3 4
5 6 7 8
9 1 2 3
X X X X   <- NOT ALLOWED, it makes the matrix square, but inserts non existing values.

or

1 2 3
4 5 6 
7 8 9 
1 2 3

For 35 Values:

1 2 3 4 5 6 7
8 9 1 2 3 4 5
6 7 8 9 1 2 3
4 5 6 7 8 9 1
2 3 4 5 6 7 8
\$\endgroup\$

closed as unclear what you're asking by Peter Taylor, Falko, Optimizer, Calvin's Hobbies, COTO Oct 21 '14 at 12:31

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ You should give more details about the values. Are they continues ? How is {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7} sorted ? How does the input look like ? What are the ways of input. \$\endgroup\$ – Optimizer Oct 21 '14 at 9:33
  • 2
    \$\begingroup\$ Is the question "Find the factor of a natural number which is closest to its square root?" or is there more to it than that? It's tagged sorting but the title implies that there's no need to sort. I am confused. \$\endgroup\$ – Peter Taylor Oct 21 '14 at 9:41
  • 1
    \$\begingroup\$ @Sempie Ah I see. I somehow read "random" as "arbitrary". \$\endgroup\$ – Martin Ender Oct 21 '14 at 9:48
  • 1
    \$\begingroup\$ If the program defines the list itself, it could simply print an arbitrary valid result without bothering about the input. I could always argue, the output corresponds to the random (non existing) input, the program thought of. \$\endgroup\$ – Falko Oct 21 '14 at 9:53
  • 2
    \$\begingroup\$ Well if you want this challenge to survive it would be best to rephrase is without the values. They distract from the main goal which as PeterTaylor said is simply to "Find the factor of a natural number which is closest to its square root?" That only needs a few sentences, and may have been done before. \$\endgroup\$ – Calvin's Hobbies Oct 21 '14 at 9:54
1
\$\begingroup\$

Mathematica, 91 bytes

n=RandomInteger@199+1
TableForm@Partition[Range@9~RandomChoice~n,Divisors@n~Nearest~Sqrt@n]

I hope I got this right:

  • Generate a random length between 1 and 200 (inclusive).
  • Generate a list of that length with numbers from 1 to 9 (inclusive).
  • Find one of the two divisors closest to the square root, with Divisors@n~Nearest~Sqrt@n.
  • Break the list into rows of that length.
  • Display the grid with TableForm.

Example run:

enter image description here

\$\endgroup\$
0
\$\begingroup\$

Javasript, 156 or 119 152 or 115

Output numbers 1 to 9: 152

for(q=~~Math.sqrt(n=new Date%200);~~(w=n/q)!=w;--q);i=q=0;alert("".replace.call(Array(n+1),/./g,function(){return(++q>9?q=1:q)+(++i==w?i=0||"\n":" ")}))

Output zeroes: 115

for(q=~~Math.sqrt(n=new Date%200);~~(w=n/q)!=w;--q);alert(Array(q+1).join("".replace.call(Array(w+1),/./g,0)+"\n"))
\$\endgroup\$

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