获取一个数的因数
我需要得到给定数字( n )的两个因子( x, y ),使得:
- x * y <= n
- x * y 应尽可能接近 n
- x 和 y 应彼此尽可能接近可能的。
示例:
- n = 16 => x = 4, y = 4
- n = 17 => x = 4, y = 4
- n = 18 => x=6,y=3n
- =20=> x = 5, y = 4
任何语言都可以,但最好是 php。
编辑 - 澄清
我想创建一个矩形, x 单位宽 * y 单位高,使其面积尽可能接近 n 。 x 和 y 必须是整数。如果 n 是质数,则 n - 1 的因数是可接受的。
I need to get two factors ( x, y ) of a given number ( n ) such that:
- x * y <= n
- x * y should be as close to n as possible
- x and y should be as close to each other as possible.
Examples:
- n = 16 => x = 4, y = 4
- n = 17 => x = 4, y = 4
- n = 18 => x = 6, y = 3
- n = 20 => x = 5, y = 4
Any language will do but preferably php.
EDIT -- CLARIFICATION
I want to create a rectangle, x units wide * y units tall such that its area is as close to n as possible. x and y must be integers. If n is a prime number then factors of n - 1 are acceptable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你的规格不够精确。您声明您想要因子,但在您的测试用例中 4 不是 17 的因子
以下伪代码优先考虑一个因子是精确
其中作为一个简单的 sqrt 语句将努力确保数字尽可能接近,但不能保证它们是因子。
Your specifications weren't quite exact enough. You stated that you wanted factors, yet in your test case 4 is not a factor of 17
The following pseudo code works prioritizing that one factor is exact
Where as a simple sqrt statement will work for ensuring that the numbers are as close together as possible, but doesn't guarantee that they are factors.
您需要确定这三个规则的重要性。
可能性1:如果x * y尽可能接近n为真,则n=17 => 1,17 不是 4,4。在这种情况下,您需要因式分解,并且有很多方法可以实现,但是像这样的代码很简单:
可能性 2: 如果彼此靠近更重要,您会期望 n=18= >4,4 而不是 3,6,并且此代码可以工作。然而,这不是因素。
如果没有更清晰的规范,所写的问题是无法解决的。
编辑 ------------
现在规范已被编辑,现在已定义,但您需要执行可能性 1,看看结果是否为质数(1 是其中之一)值),然后重复执行可能性 2。但是,我怀疑这是否是哪个老师写的家庭作业的目的。
You need to decide how important your three rules are.
Possibility 1: If x * y being as close to n as possible is true then n=17 => 1,17 not 4,4. In this case you want factorisation and there are lots of ways to do it, but code like this is simple:
Possibility 2: If being close to each other is more important you'd expect n=18=>4,4 rather than 3,6, and this code would work. This however is not factors.
The problem as written is unsolvable without a clearer specification.
EDIT ------------
Now the spec has been edited it is now defined, but you need to do Possibility 1, see if the result is prime (1 is one of the values) and then if it is repeat doing Possibility 2. However, I doubt this is what whichever teacher wrote this as homework intended.
我的一个想法(比 php 更伪)
An idea from me (more pseudo then php)
我将使用以下代码将所有因素写入数组。
然后我会循环遍历数组以检查哪些实际上可以使用。有关此算法的更多信息,请查看 http ://pyfon.blogspot.com.au/2012/09/list-factors-of-number-in-python.html
I'd have all the factors written to an array using the following code.
Then I'd loop through the array to check which ones can actually be used. For more on this algorithm, check out http://pyfon.blogspot.com.au/2012/09/list-factors-of-number-in-python.html
这是一个 PHP 函数,它优先考虑两个彼此接近的“因素”而不是精确的因素:
Here is a PHP function that prioritize the two 'factors' being close to each other over having exact factors:
编写一个程序来查找任意数字的因数
Write a program to find factor of any number