保证金太窄


30

大约在1637年左右,Pierre de Fermat在他的算术副本的空白处写道:

It is impossible to separate a cube into two cubes, or a fourth power 
into two fourth powers, or in general, any power higher than the 
second, into two like powers. I have discovered a truly marvelous 
proof of this, which this margin is too narrow to contain.

对于我们来说不幸的是,利润率仍然太窄而无法容纳证明。今天,我们将在空白处编写一个简单的程序,以确认任意输入的证明。

挑战

我们想要一个给定功能的函数程序,将其分成两个尽可能接近该功率的两对。我们希望执行此操作的程序尽可能小,以使其适合页边空白。


输入值

功率和功率号:cx

约束:c > 2x > 2

输入可以通过程序参数,函数参数或来自用户。

输出量

这种精确的字符串:“ a^x + b^x < c^x”有abc,并x与它们的字面整数值代替。a并且b必须选择,使a^x + b^x < c^x的,没有其他价值ab将使其更加接近c^x。也:a>=b>0

输出可以通过函数返回值,stdout,保存到文件或在屏幕上显示。


例子:

> 3 3
2^3 + 2^3 < 3^3
> 4 3
3^3 + 3^3 < 4^3
> 5 3
4^3 + 3^3 < 5^3
> 6 3
5^3 + 4^3 < 6^3
> 7 3
6^3 + 5^3 < 7^3
> 8 3
7^3 + 5^3 < 8^3

由于费马(Fermat)具有一般的写作能力,因此不允许使用无法打印的字符。字符数最少的程序获胜。


排行榜

这是一个堆栈片段,用于按语言生成常规排行榜和获胜者概述。

为确保您的答案显示出来,请使用以下Markdown模板以标题开头。

## Language Name, N characters

或者,您可以从以下内容开始:

## Language Name, N bytes

N您提交的文件大小在哪里。如果您提高了分数,则可以将旧分数保留在标题中,方法是将它们打掉。例如:

## Ruby, <s>104</s> <s>101</s> 96 bytes

如果要在标头中包含多个数字(例如,因为您的分数是两个文件的总和,或者您想单独列出解释器标志罚分),请确保实际分数是标头中的最后一个数字:

## Perl, 43 + 2 (-p flag) = 45 bytes

您还可以将语言名称设置为链接,然后该链接将显示在页首横幅代码段中:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes


1
我认为应该是这样,a>=b>0否则您的第一个示例将是无效的。为什么我们一定要显示<,当你希望它是<=
瑕疵

@flawr已修复:)
TheNumberOne 2015年

以相反的顺序接受参数是否可以?首先x,再c
Reto Koradi 2015年

@RetoKoradi当然:)
TheNumberOne 2015年

Answers:


9

Pyth, 38 bytes

Ls^Rvzbjd.imj\^,dz+eoyNf<yTy]Q^UQ2Q"+<

Takes input in this format:

x
c

8

Matlab, 169 153 bytes

The score can be +-1 depending on the unsolved problems in the comments=) The score stays the same. This is just a bruteforce search for the best (a,b) pair.

Pretty disappointing: I first tried experimented with some 'fancy' stuff and then realized two simple nested for loops are way shorter...

function f(c,x);
m=0;p=@(x)int2str(x);
X=['^' p(x)];
for b=1:c;for a=b:c;
n=a^x+b^x;
if n<c^x&n>m;m=n;s=[p(a) X ' + ' p(b) X ' < ' p(c) X];end;end;end;
disp(s)

Old version:

function q=f(c,x);
[b,a]=meshgrid(1:c);
z=a.^x+b.^x;
k=find(z==max(z(z(:)<c^x & a(:)>=b(:))),1);
p=@(x)int2str(x);x=['^' p(x)];
disp([p(a(k)) x ' + ' p(b(k)) x ' < ' p(c) x])

Remove the spaces in m = 0? Still, that won't get you close to my answer :-PP
Luis Mendo

Also, it looks like you could remove q= from the function definition
Luis Mendo

I don't see the q variable being used anywhere. You can shave off a couple of bytes by simply doing function f(c,x) and removing the semi-colon as well.
rayryeng - Reinstate Monica

8

Mathematica, 79 95 80 bytes

This just might fit on the margin.

c_~f~x_:=Inactivate[a^x+b^x<c^x]/.Last@Solve[a^x+b^x<c^x&&a>=b>0,{a,b},Integers]

Testing

f[3, 3]
f[4, 3]
f[5, 3]
f[6, 3]
f[7, 3]
f[8, 3]

output


7

CJam, 51 46 43 bytes

q~_2m*\f+{W$f#)\:+_@<*}$W='^@s+f+"+<".{S\S}

This full program reads the power, then the base from STDIN.

Try it online in the CJam interpreter.


6

Matlab, 141 140 bytes

This is coded as function that displays the result in stdout.

function f(c,x)
b=(1:c).^x;d=bsxfun(@plus,b,b');d(d>c^x)=0;[~,r]=max(d(:));sprintf('%i^%i + %i^%i < %i^%i',[mod(r-1,c)+1 ceil(r/c) c;x x x])

Example use:

>> f(8,3)
ans =
7^3 + 5^3 < 8^3

Or try it online in Octave.

Thanks to @flawr for removing one byte.


I always avoided sprintf because it seemed so complicated while actually it really isn't! And I forgot about bsxfun once again, so thats a very elegant solution. I especially like the way you abused the single/double indexing in the last argument=) (You could remove a space there too!)
flawr

Thanks! I usually use disp too, except in Code Golf :-P
Luis Mendo

If you use fprintf instead of sprintf, it doesn't display "ans"
Jonas

@Jonas But it prints the result and then the prompt >> in the same line, which is a bit strange
Luis Mendo

You can use fprintf, but you'd have to insert a manual carriage return.
rayryeng - Reinstate Monica

5

CJam, 53 51 bytes

l~:C\:X#:U;C2m*{Xf#:+_U<*}$W=~"^"X+:T" + "@T" < "CT

Try it online

The input format is x c, which is the reverse of the order used in the examples.

Explanation:

l~    Read and interpret input.
:C    Store c in variable C.
\     Swap x to top.
:X    Store it in variable X.
#     Calculate c^x.
:U;   Store it in variable U as the upper limit, and pop it from stack.
C2m*  Generate all pairs of values less than c. These are candidates for a/b.
{     Start of mapping function for sort.
  X     Get value of x.
  f#    Apply power to calculate [a^x b^x] for a/b candidates.
  :+    Sum them to get a^x+b^x.
  _U<   Compare value to upper limit.
  *     Multiply value and comparison result to get 0 for values above limit.
}$    End of sort block.
W=    Last a/b pair in sorted list is the solution.
~     Unpack it.
"^"X+ Build "^x" string with value of x.
:T    Store it in variable T, will use it 2 more times in output.
" + " Constant part of output.
@     Rotate b to top of stack.
T     "^x" again.
" < " Constant part of output.
C     Value of c.
T     And "^x" one more time, to conclude the output.

5

R, 139 characters

function(c,x)with(expand.grid(a=1:c,b=1:c),{d=a^x+b^x-c^x
d[d>0]=-Inf
i=which.max(d)
sprintf("%i^%4$i + %i^%4$i < %i^%4$i",a[i],b[i],c,x)})

4

Python 2, 182 161 157 bytes

I usually answer in MATLAB, but because there are already two solutions in that language, I'd figure I'd try another language :)

def f(c,x):print max([('%d^%d + %d^%d < %d^%d'%(a,x,b,x,c,x),a**x+b**x) for b in range(1,c+1) for a in range(b,c+1) if a**x+b**x<c**x],key=lambda x:x[1])[0]

Ungolfed code with explanations

def f(c,x): # Function declaration - takes in c and x as inputs

    # This generates a list of tuples where the first element is 
    # the a^x + b^x < c^x string and the second element is a^x + b^x
    # Only values that satisfy the theorem have their strings and their
    # corresponding values here
    # This is a brute force method for searching
    lst = [('%d^%d + %d^%d < %d^%d'%(a,x,b,x,c,x),a**x+b**x) for b in range(1,c+1) for a in range(b,c+1) if a**x+b**x<c**x]

    # Get the tuple that provides the largest a^x + b^x value
    i = max(lst, key=lambda x:x[1])

    # Print out the string for this corresponding tuple
    print(i[0])

Example Runs

I ran this in IPython:

In [46]: f(3,3)
2^3 + 2^3 < 3^3

In [47]: f(4,3)
3^3 + 3^3 < 4^3

In [48]: f(5,3)
4^3 + 3^3 < 5^3

In [49]: f(6,3)
5^3 + 4^3 < 6^3

In [50]: f(7,3)
6^3 + 5^3 < 7^3

In [51]: f(8,3)
7^3 + 5^3 < 8^3

Try it online!

http://ideone.com/tMjGdh

If you want to run the code, click on the edit link near the top, then modify the STDIN parameter with two integers separated by a space. The first integer is c and the next one is x. Right now, c=3 and x=3 and its result is currently displayed.


3

Pyth, 53 52 50 bytes

Ls^ReQbs[j" + "+R+\^eQ_Sh.MyZf<yT^FQ^UhQ2" < "j\^Q

Try it online.

Takes as input c,x where c is the target number and x is the base.



2

C, 175 bytes

a,b,m,A,B,M;p(
a,x){return--x
?a*p(a,x):a;}f
(c,x){M=p(c,x)
;for(a=c,b=1;a
>=b;)(m=p(c,x)
-p(a,x)-p(b,x
))<0?--a:m<M?
(M=m,B=b++,A=
a):b++;printf
("%d^%d + %d"
"^%d < %d^%d",
A,x,B,x,c,x);}

To fit the code into the margin, I've inserted newlines and split a string literal above - the golfed code to be counted/compiled is

a,b,m,A,B,M;p(a,x){return--x?a*p(a,x):a;}f(c,x){M=p(c,x);for(a=c,b=1;a>=b;)(m=p(c,x)-p(a,x)-p(b,x))<0?--a:m<M?(M=m,B=b++,A=a):b++;printf("%d^%d + %d^%d < %d^%d",A,x,B,x,c,x);}

Function f takes c and x as arguments, and produces the result on stdout.

Explanation

This is an iterative solution that zigzags the line defined by a^x + b^x = c^x. We start with a=c and b=1. Obviously, that puts us on the wrong side of the line, because c^x + 1 > c^x. We decrement a until we cross the line. When we're below the line, we increment b until we cross it in the other direction. Repeat until b meets a, remembering the best solution in A and B as we go. Then print it.

p is a simple recursive implementation of a^x (for x>0) since C provides no operator for exponentiation.

In pseudo-code:

a=c
b=1
M = c^x

while a >= b
do
   m = c^x - a^x - b^x
   if m < 0
      a -= 1
   else // (m > 0, by Fermat's Last Theorem)
      if m < M
         A,B,M = a,b,m
      b += 1
done
return A,B

Limitations

c^x must be representable within the range of int. If that limitation is too restrictive, the signature of p could be trivially modified to long p(long,int) or double p(double,int), and m and M to long or double respectively, without any modification to f().

Test program

This accepts c and x as command-line arguments, and prints the result.

#include<stdio.h>
int main(int argc, char**argv) {
    if (argc <= 2) return 1;
    int c = atoi(argv[1]);
    int x = atoi(argv[2]);
    f(c,x);
    puts("");
    return 0;
}

1

Haskell, 120 bytes

I think I've golfed this as much as I can:

c%x=a&" + "++b&" < "++c&""where(_,a,b)=maximum[(a^x+b^x,a,b)|b<-[1..c],a<-[b..c],a^x+b^x<c^x];u&v=show u++"^"++show c++v

Ungolfed:

fn c x = format a " + " ++ format b " < " ++ format c ""
    where format :: Integer -> String -> String
          -- `format u v` converts `u`, appends an exponent string, and appends `v`
          format u v = show u ++ "^" ++ show c ++ v
          -- this defines the variables `a` and `b` above
          (_, a, b) = maximum [(a^x + b^x, a, b) | b <- [1..c], 
                                                   a <- [b..c],
                                                   a^x + b^x < c^x]

Usage:

Prelude> 30 % 11
"28^30 + 28^30 < 30^30"

0

Haskell, 132 128 bytes

x!y=x++show y
c#x=(\[_,a,b]->""!a++"^"!x++" + "!b++"^"!x++" < "!c++"^"!x)$maximum[[a^x+b^x,a,b]|a<-[0..c],b<-[0..a],a^x+b^x<c^x]

Usage example: 7 # 3 returns the string "6^3 + 5^3 < 7^3".


0

Perl 5, 119 bytes

A subroutine:

{for$b(1..($z=$_[0])){for(1..$b){@c=("$b^$o + $_^$o < $z^$o",$d)if($d=$b**($o=$_[1])+$_**$o)<$z**$o and$d>$c[1]}}$c[0]}

Use as e.g.:

print sub{...}->(8,3)

0

Ruby, 125 bytes

Anonymous function. Builds a list of a values, uses it to construct a,b pairs, then finds the max for the ones that fit the criteria and returns a string from there.

->c,x{r=[];(1..c).map{|a|r+=([a]*a).zip 1..a}
a,b=r.max_by{|a,b|z=a**x+b**x;z<c**x ?z:0}
"#{a}^#{x} + #{b}^#{x} < #{c}^#{x}"}
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.