获取一个数的因数

发布于 2024-08-13 17:14:00 字数 415 浏览 5 评论 0原文

我需要得到给定数字( 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

神妖 2024-08-20 17:14:00

你的规格不够精确。您声明您想要因子,但在您的测试用例中 4 不是 17 的因子

以下伪代码优先考虑一个因子是精确

for i in range(ceiling(sqrt(n)), 1){
    if ( n modulo i ) == 0 {
          x = i
          y = round(n/i)
    }
}

其中作为一个简单的 sqrt 语句将努力确保数字尽可能接近,但不能保证它们是因子。

x = y = round( sqrt(n) )

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

for i in range(ceiling(sqrt(n)), 1){
    if ( n modulo i ) == 0 {
          x = i
          y = round(n/i)
    }
}

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.

x = y = round( sqrt(n) )
瀞厅☆埖开 2024-08-20 17:14:00

您需要确定这三个规则的重要性。

可能性1:如果x * y尽可能接近n为真,则n=17 => 1,17 不是 4,4。在这种情况下,您需要因式分解,并且有很多方法可以实现,但是像这样的代码很简单:

for(i = floor(sqrt(n)) .. 1) {
  if n % i ==0 {
     x = i;
     y = n/x;
     break;
  }
}

可能性 2: 如果彼此靠近更重要,您会期望 n=18= >4,4 而不是 3,6,并且此代码可以工作。然而,这不是因素。

x=floor(sqrt(n))
y=floor(n/x)

如果没有更清晰的规范,所写的问题是无法解决的。

编辑 ------------

现在规范已被编辑,现在已定义,但您需要执行可能性 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:

for(i = floor(sqrt(n)) .. 1) {
  if n % i ==0 {
     x = i;
     y = n/x;
     break;
  }
}

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.

x=floor(sqrt(n))
y=floor(n/x)

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.

话少心凉 2024-08-20 17:14:00
$num = ...; // some number

if (is_prime($num)) // implement the is_prime() function yourself
    --$num; // Subtract to get an even number, which is not a prime

$candidates = array();  // Numbers that may fit.

$top_search = $num / 2; // Limits the useless search for candidates

for($i=1; $i < $top_search; ++$i)
{
    if ($num % $i == 0)
        $candidates[$i] = $num / $i;
}

// Now, check the array in the middle 
$num = ...; // some number

if (is_prime($num)) // implement the is_prime() function yourself
    --$num; // Subtract to get an even number, which is not a prime

$candidates = array();  // Numbers that may fit.

$top_search = $num / 2; // Limits the useless search for candidates

for($i=1; $i < $top_search; ++$i)
{
    if ($num % $i == 0)
        $candidates[$i] = $num / $i;
}

// Now, check the array in the middle 
睫毛溺水了 2024-08-20 17:14:00

我的一个想法(比 php 更伪)

$root = sqrt($inputNumber);

$x = floor($root);
$y = floor($root);

if(($root - $x) > 0.5) $y++;

An idea from me (more pseudo then php)

$root = sqrt($inputNumber);

$x = floor($root);
$y = floor($root);

if(($root - $x) > 0.5) $y++;
山有枢 2024-08-20 17:14:00

我将使用以下代码将所有因素写入数组。

#Application lists all factors/divisors for a number.
targetNumber=input('What number do you want the factors for?\n> ')
factors=[]
for i in range(1,targetNumber):
    if targetNumber%i==0:
        factors.append(i)
    elif targetNumber/i==1:
        factors.append(targetNumber)
        break
print factors

然后我会循环遍历数组以检查哪些实际上可以使用。有关此算法的更多信息,请查看 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.

#Application lists all factors/divisors for a number.
targetNumber=input('What number do you want the factors for?\n> ')
factors=[]
for i in range(1,targetNumber):
    if targetNumber%i==0:
        factors.append(i)
    elif targetNumber/i==1:
        factors.append(targetNumber)
        break
print factors

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

始终不够 2024-08-20 17:14:00

这是一个 PHP 函数,它优先考虑两个彼此接近的“因素”而不是精确的因素:

function weird_factors($ori) {
    $sq = intval(sqrt($ori));
    $start = $sq - 10;
    $end = $sq + 10;
    $n = 0;
    for ($s = $start; $s <= $end; $s++) {
        for ($t = $start; $t <= $end; $t++) {
            $st = $s * $t;
            if ($st <= $ori and $st > $n) {
                $n = $st;
                $ns = $s;
                $nt = $t;
            }
        }
    }
    return array($ns, $nt);
}

Here is a PHP function that prioritize the two 'factors' being close to each other over having exact factors:

function weird_factors($ori) {
    $sq = intval(sqrt($ori));
    $start = $sq - 10;
    $end = $sq + 10;
    $n = 0;
    for ($s = $start; $s <= $end; $s++) {
        for ($t = $start; $t <= $end; $t++) {
            $st = $s * $t;
            if ($st <= $ori and $st > $n) {
                $n = $st;
                $ns = $s;
                $nt = $t;
            }
        }
    }
    return array($ns, $nt);
}
黯然 2024-08-20 17:14:00

编写一个程序来查找任意数字的因数

<?php
if(isset($_POST['sub']))
 {     $j=0;
   $factor=array(); 
   $num=$_POST['nm1'];
   for($i=1;$i<=$num;$i++)  
       {
          if($num%$i==0)
            { 
             $j++;
             $factor[$j]=$i;
            }
       }
}
 ?>

 <table>
 <form name="frm" method="post" action="">
 <tr> <td>Number:</td> <td><input type="text" name="nm1" /></td> </tr>
 <tr><td></td><td><input type="submit" name="sub" /></td>
 <td><center><span>  
  <?php   
    if(isset($_POST['sub']))  
    { 
       echo "Factors are :";for($i=1;$i<=count($factor);$i++) 
       {          echo $factor[$i].",";

        }
     }       
  ?>   
   </span></center></td></tr>
 </form>
 </table>

Write a program to find factor of any number

<?php
if(isset($_POST['sub']))
 {     $j=0;
   $factor=array(); 
   $num=$_POST['nm1'];
   for($i=1;$i<=$num;$i++)  
       {
          if($num%$i==0)
            { 
             $j++;
             $factor[$j]=$i;
            }
       }
}
 ?>

 <table>
 <form name="frm" method="post" action="">
 <tr> <td>Number:</td> <td><input type="text" name="nm1" /></td> </tr>
 <tr><td></td><td><input type="submit" name="sub" /></td>
 <td><center><span>  
  <?php   
    if(isset($_POST['sub']))  
    { 
       echo "Factors are :";for($i=1;$i<=count($factor);$i++) 
       {          echo $factor[$i].",";

        }
     }       
  ?>   
   </span></center></td></tr>
 </form>
 </table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文