为具有n个元素并为其比例指定范围的表找到最佳的列和行大小 [英] Finding the optimum column and row size for a table with n elements and a given range for its proportion

查看:89
本文介绍了为具有n个元素并为其比例指定范围的表找到最佳的列和行大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种由n个元素创建表格的最佳方法,以使理想情况下没有空单元格,但与此同时,表格尺寸的列/行的比例应尽可能接近1. >

当然,如果n是一个平方数,那么从那以后很容易

cols = rows = sqrt( n );

如果n是素数,则很明显会有空单元格,所以我目前的处理方式是:

rows = floor( sqrt(n) );
cols = ceil( n / rows  );

对于所有其他情况,我的计划是获取n的素因子,然后搜索其组合的比例最接近1的那些可能的排列.

所以我的问题是:是否有更好的方法来做到这一点?还是至少有一种方法不必测试主要因素的每种可能组合?

解决方案

不是进行素数分解n,而是从平方根开始并查找下一个较大的(或较小的-无差异)因数.这对因子将最接近平方根,因此最接近1:1的比例.

I am looking for an optimum way to create a table from n elements so that ideally there are no empty cells, but at the same time the proportion of the table dimensions columns / rows becomes as close to 1 as possible.

Of course if n is a square number it is easy since then

cols = rows = sqrt( n );

If n is a prime number it is also clear that there will be empty cells, so my current way to handle this is:

rows = floor( sqrt(n) );
cols = ceil( n / rows  );

For all other cases my plan is to get the prime factors of n and then search all possible permutations for those whose combination has proportions closest to 1.

So my question is: is there is a better way to do this? Or is there at least a way not having to test every possible combination of the prime factors?

解决方案

Instead of doing a prime factorization of n, start from the square root and find the next larger (or smaller -- makes no difference) factor. That pair of factors will the closest to the square root, and therefore the closest to a proportion of 1:1.

这篇关于为具有n个元素并为其比例指定范围的表找到最佳的列和行大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆