曲解单项式


9

有一个方程,假设nx为正,

方程

表示两个单项式之间的关系,一个是另一种的常见错误陈述。许多人犯了一个简单的错误,即将它们等同(即3x^2(3x)^2)。

挑战

给定一个正整数,i确定并返回总和最小的解决方案nx数组)[n, x]。在平局的情况下,任何解决方案集都是可以接受的。

测试用例

62658722541234765
[15, 11]

202500
[4, 15]

524288
[8, 4]

33044255768277
[13, 9]

我们可以[x, n]代替[n, x]吗?
致命

另外,有时间限制吗?
致命

nx整数,对不对?
路易斯·门多

输出采用以下格式,[n, x]并且没有时间限制@Fatalize
Zach Gates

是的,n并且x是整数@LuisMendo
Zach Gates

Answers:


5

Brachylog,35个字节

,[N:X]#>>==L(.rMtT;Lr.rMtT),M^:T*?,

在线尝试!

说明

我们构造一个list [N, X],其中N >= X,然后为它分配值之后,尝试两者[N, X][X, N]尽可能输出。例如,如果N被分配到3,我们将通过回溯测试[3, 1][1, 3][3, 2][2, 3][3, 3][3, 3]。之后,下一个回溯步骤将发生在的值上N,该值将转到4,等等。

,[N:X]     The list [N, X]
#>         Both N and X are strictly positive
>=         N >= X
=L         Assign values to N and X, and L = [N, X]
(          Either...
    .          Output = L
    rM         M is the reverse of the Output
    tT         T is the second element of M
;          ...or...
    Lr.        Output is the reverse of L
    rM         M = L
    tT         T is the last element of M
),
M^         First element of M to the power of the second element of L (T)...
:T*?,      ... times T is equal to the Input

5

Mathematica,61个字节

多亏了Miles节省了2个字节,再加上我无缘无故计数的一整个字节!

Last@Select[{n,(#/n)^(1/n)}~Table~{n,2Log@#},IntegerQ@*Last]&

使用所有可能的n值计算对{n,x}的表,其中x =(i / n)^(1 / n);仅保留对应的x为整数的整数;然后返回具有最大n的对。

在此,“ n的所有可能值”的范围为1至2 * ln(i)。这将忽略解决方案{n,x} = {i,1},但这没关系,因为如果解决方案{n,x} = {1,i}是最佳选择就足够了。因此,x永远不必小于2,这意味着n * 2 ^ n≤i,并且所有此类n均小于2 * ln(i)。

使用微积分可以表明,在这种情况下,将其总和最小化的对{n,x}与具有最大n(不计算{i,1})的对{n,x}是相同的。这就是为什么初始Last值足以找到最佳对的原因。


1
您可以使用IntegerQ@*Last2个字节来构成测试条件,但是在当前版本中,我还算出63个字节而不是86个字节。
英里

3

MATL,22字节

`T@XK:"@K@-@^*G=?3Mb~.

输出是x,按此n顺序。

输入受MATL的默认double数据类型限制,该数据类型可以准确地表示最多整数2^53。这不包括第一个测试(仍然可以提供正确的结果,但是对于如此大的输入通常无法保证)。

在线尝试!

说明

该代码使用两个嵌套循环:

  • do...while循环按升序遍历所有可能的和n+x。找到解决方案后,循环将停止。这保证了我们以最小的总和输出解决方案。
  • for each循环测试所有n,并x用该款项。当总和与输入一致时,将退出内部循环,并将外部循环的循环条件设置为,false从而也退出一个循环。

注释代码:

`         % Do...while
  T       %   Push "true". Will be used as loop condition (possibly negated to exit loop)
  @       %   Push iteration index, say K, which represents n+x
  XK      %   Copy that into clipboard K
  :       %   Range [1 2 ... K]
  "       %   For each
    @     %     Push loop variable (1, 2, ... K), which represents n
    K@-   %     Compute x as K minus n
    @     %     Push n again
    ^*    %     Power, multiply. This gives n*x^n
    G=    %     Does this equal the input?
    ?     %     If so
      3M  %       Push the inputs of the third-last function, that is, x and n
      b   %       Bubble up the "true" that is at the bottom of the stack
      ~   %       Transform it into "false". This will exit the do...while loop
      .   %       Break the for loop
          %     Implicitly end if
          %   Implicitly end for
          % Implicitly end do...while
          % Implicitly display

2

果冻23 16字节

×*@¥/=³
ṗ2ÇÐfSÞḢ

给定的话i,它将生成所有整数对,并在中替换[1, i]。然后,它执行与下面显示的先前解决方案相同的过滤和排序。由于没有时间限制,因此蛮力将在足够的时间下起作用。

在线尝试!,但不要在线尝试较大的价值。

在我的电脑上,大约需要6分钟才能计算出i = 2048使用低效率版本的结果。

高效版

这是先前针对23个字节的解决方案,能够快速解决较大的值。

×*@¥/=³
ÆDµṚ*İ⁸żḞÇÐfSÞḢ

鉴于i,计算的除数i,以产生对[n, x]这里n是一个除数和x = floor( (i/n)^(1/n) )。然后对其中的值进行过滤n * x^n == i,将剩余的对按其总和排序,然后返回第一对。

在线尝试!验证所有测试用例。

说明

×*@¥/=³  Helper link. Input: list [n, x]
    /    Reduce using
   ¥       A dyadic chain
 *@        Compute x^n
×          Multiply by n
      ³  The initial value i
     =   Test if n * x^n == i

ṗ2ÇÐfSÞḢ  Main link (16 byte version). Input: integer i
ṗ2        Generate all pairs of integers in [1, i]
  ÇÐf     Filter for where the helper link is true
     SÞ   Sort them by their sum
       Ḣ  Return the first result

ÆDµṚ*İ⁸żḞÇÐfSÞḢ  Main link (23 byte version). Input: integer i
ÆD               Compute the divisors of i
  µ              Begin a new monadic chain operating on the divisors
   Ṛ             Reverse the divisors
     İ           Reciprocal of each divisors
    *            Raise each in the reversed divisors to the reciprocal of a divisor
      ⁸          Get the divisors
       ż         Interleave the divisors with the previous powers
        Ḟ        Floor each
         ÇÐf     Filter for where the helper link is true
            SÞ   Sort them by their sum
              Ḣ  Return the first result

1

PHP,104字节

for(;1<$x=(($i=$argv[1])/++$n)**(1/$n);)!($x==ceil($x))?:$a[$x+$n]="[$x, $n]";ksort($a);echo$a[key($a)];

这将输出所有非建议格式的可能解决方案73字节

for(;1<=$x=(($i=$argv[1])/++$n)**(1/$n);)!($x==ceil($x))?:print"$x,$n\n";

1

Perl,52个字节

包括+2 -ap

在STDIN上输入

mono.pl <<< 33044255768277

mono.pl

#!/usr/bin/perl -ap
$_=("@F"/++$.)**(1/$.)while!/\./?$\="$. $_":$_>2}{

付出了一些努力使其也起作用1。我不知道浮点错误是否可以使某些输入返回错误的答案。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.