フェルマーニアミス


31

フェルマーの最後の定理によると、の方程式には正の積分解はありませa^n + b^n = c^nn>2。これは、1994年にAndrew Wilesによって真実であることが証明されました。

ただし、ディオファントスの式をほぼ満たすが、1つだけ逃す「ニアミス」が多くあります。正確には、それらはすべて1より大きく、の整数解ですa^3 + b^3 = c^3 + 1(シーケンスは、方程式の各辺の値で、昇順です)。

このシーケンスのn最初のn値を出力するタスクが与えられます。

シーケンスの最初のいくつかの値は次のとおりです。

1729, 1092728, 3375001, 15438250, 121287376, 401947273, 3680797185, 6352182209, 7856862273, 12422690497, 73244501505, 145697644729, 179406144001, 648787169394, 938601300672, 985966166178, 1594232306569, 2898516861513, 9635042700640, 10119744747001, 31599452533376, 49108313528001, 50194406979073, 57507986235800, 58515008947768, 65753372717929, 71395901759126, 107741456072705, 194890060205353, 206173690790977, 251072400480057, 404682117722064, 498168062719418, 586607471154432, 588522607645609, 639746322022297, 729729243027001

これはなので、バイト単位の最短コードが勝ちです!



1
1つ目は、ラマヌジャンのen.wikipedia.org/wiki/Taxicab_numberです。cのシーケンス、oeis.org / A050791役立つ場合があります。
JollyJoker 16

回答:


14

ゼリー、16バイト

*3‘
ḊŒc*3S€ċǵ#Ç

ブルートフォースソリューション。オンラインでお試しください!

*3‘           Helper link. Maps r to r³+1.

ḊŒc*3S€ċǵ#Ç  Main link. No arguments.

         µ    Combine the links to the left into a chain.
          #   Read an integer n from STDIN and execute the chain to the left for
              k = 0, 1, 2, ... until n matches were found. Yield the matches.
Ḋ             Dequeue; yield [2, ..., k].
 Œc           Yield all 2-combinations of elements of that range.
   *3         Elevate the integers in each pair to the third power.
     S€       Compute the sum of each pair.
        Ç     Call the helper link, yielding k³+1.
       ċ      Count how many times k³+1 appears in the sums. This yields a truthy 
              (i.e., non-zero) integer if and only if k is a match.
           Ç  Map the helper link over the array of matches.

8

Brachylog、31バイト

:{#T#>>:{:3^}aLhH,Lb+.-H,#T=,}y

オンラインでお試しください!

これは制約を使用するため、完全なブルートフォースではありません。これは、TIOで少し遅い(約20秒N = 5)。私のマシンで約5秒N = 5、13秒かかりN = 6ます。

説明

:{                           }y    Return the first Input outputs of that predicate
  #T                               #T is a built-in list of 3 variables
    #>                             #T must contain strictly positive values
      >                            #T must be a strictly decreasing list of integers
       :{:3^}aL                    L is the list of cubes of the integers in #T
              LhH,                 H is the first element of L (the biggest)
                  Lb+.             Output is the sum of the last two elements of L
                     .-H,          Output - 1 = H
                         #T=,      Find values for #T that satisfy those constaints

8

Perl、78バイト

#!perl -nl
grep$_<(($_+2)**(1/3)|0)**3,map$i**3-$_**3,2..$i++and$_-=print$i**3+1while$_

ブルートフォースアプローチ。シバンを2つとして計算すると、入力はstdinから取得されます。

サンプルの使用法

$ echo 10 | perl fermat-near-miss.pl
1729
1092728
3375001
15438250
121287376
401947273
3680797185
6352182209
7856862273
12422690497

オンラインでお試しください!


7

Mathematica、95バイト

(b=9;While[Length[a=Select[Union@@Array[#^3+#2^3&,{b,b},2],IntegerQ[(#-1)^3^-1]&,#]]<#,b++];a)&

単一の正の整数引数を取り#、必要な#整数のリストを返す名前のない関数。人間が読みやすいように間隔を空けています:

1  (b = 9; While[
2    Length[ a =
3      Select[
4        Union @@ Array[#^3 + #2^3 &, {b, b}, 2],
5        IntegerQ[(# - 1)^3^-1] &
6      , #]
7    ] < #, b++
8  ]; a) &

行4は、2〜b+1の整数の立方体のすべての可能な合計を(b=9行1 の初期化で)ソートされた順序で計算します。行3〜5は、これらの合計から、完全なキューブよりも1つ多い合計のみを選択します。せいぜいにそのリストライン6つの限界#に格納された値、a。しかし、実際にこのリストの#値が値よりも少ない場合、While1〜7行目のループが増分bして再試行します。最後に、8行目aが適切な長さになると出力されます。

聖地獄このバージョンは遅い!1バイト余分に、b++7行目を変更してb*=9、妥当な時間で実際にコードを実行することができます(実際、テスト方法です)。


6

ラケット166バイト

(let((c 0)(g(λ(x)(* x x x))))(for*((i(in-naturals))(j(range 1 i))(k(range j i))#:final(= c n))
(when(=(+(g j)(g k))(+ 1(g i)))(displayln(+ 1(g i)))(set! c(+ 1 c)))))

ゴルフをしていない:

(define (f n)
  (let ((c 0)
        (g (λ (x) (* x x x))))
    (for* ((i (in-naturals))
           (j (range 1 i))
           (k (range j i))
           #:final (= c n))
      (when (= (+ (g j) (g k))
               (+ 1 (g i)))
        (displayln (+ 1(g i)))
        (set! c (add1 c))))))

テスト:

(f 5)

出力:

1729
1092728
3375001
15438250
121287376


5

パリ/ GP、107バイト

F(n)=c=2;while(n>0,c++;C=c^3+1;a=2;b=c-1;while(a<b,K=a^3+b^3;if(K==C,print(C);n--;break);if(K>C, b--,a++)))

10秒で最初の10個のソリューションを見つけます。

目標: a ^ 3 + b ^ 3 = c ^ 3 + 1

  1. 関数引数nによって必要なソリューションの数を取得します

  2. c3から増やし、各c ^ 3 + 1 に対してa ^ 3 + b ^ 3 = c ^ 3 + 1になるように 1 <a <= b <cでabを 検索します。見つかった場合、さらにsoulutionsの必要数を減らすNにより1 及びリピート

  3. さらに必要なソリューションの数(n内)が0に等しい場合に終了します

それ呼び出して、最初の10個のソリューションを取得します。

F(10)

読み取り可能なコード(関数のブロック表記のインジケータとして、先頭と末尾の中括弧が必要です。また、便宜上、ソリューションのすべての変数を出力します):

{F(m) = c=2;
   while(m>0,        
     c++;C=c^3+1;             
     a=2;b=c-1;                
     while(a<b,                
           K=a^3+b^3;               
            if(K==C,print([a,b,c,C]);m--;break);
            if(K>C, b--,a++);
          );
    );}

パリ/ GP、93バイト

(デニスによる改善)

F(n)=c=2;while(n,C=c^3+1;a=2;b=c++;while(a<b,if(K=a^3+b^3-C,b-=K>0;a+=K<0,print(C);n--;b=a)))              

PPCGへようこそ!私はあなたに通常の形式に答える自由を与えました(一部のユーザースクリプトとスタックスニペットはそれに依存しています)。これにより、数バイト節約されるようです。
デニス

デニス、フォーマットについてありがとうございます。そして、削減は本当にクールです!特定の調整を見たことはありません...それをバージョンとして答えに入れます。
ゴットフリードヘルムズ

5

パイソン2、122の 119バイト

なぜまだ賛成ですか デニスはこの答えをつぶしました;)

この質問に対する最も長い解決策へようこそ。

x,y,z=2,3,4
n=input()
while n:
 if y**3+x**3-z**3==1and x<y<z:print z**3+1;n-=1
 x+=1
 if y<x:y+=1;x=2
 if z<y:z+=1;y=3

の出力n = 5

1729
1092728
3375001
15438250
121287376

4

TI-Basic、90バイト

もっと短い方法が必要です...

Prompt N
2->X
3->Y
4->Z
While N
If 1=X³+Y³-Z³ and X<Y and Y<Z
Then
DS<(N,0
X+1->X
If Y<X
Then
2->X
Y+1->Y
End
If Z<Y
Then
3->Y
Z+1->Z
End
End

2

MATLAB、94バイト

別のブルートフォースソリューション:

for z=4:inf,for y=3:z,for x=2:y,c=z^3+1;if x^3+y^3==c,n=n-1;c,if~n,return,end,end,end,end,end

の出力n=4

>> n=4; fermat_near_misses    
c =
        1729
c =
     1092728
c =
     3375001
c =
    15438250

c=ディスプレイの一部を非表示にすると、コードが100バイトに増えます

for z=4:inf,for y=3:z,for x=2:y,c=z^3+1;if x^3+y^3==c,n=n-1;disp(c),if~n,return,end,end,end,end,end

>> n=4; fermat_near_misses_cleandisp    
        1729
     1092728
     3375001
    15438250

なぜ5つの「終了」があるのですか?申し訳ありませんが、私はMathWorks社のMATLABでひどいよ
ev3commander

@ ev3commanderそれは、MATLABのステートメントの終了記号であり、「終了かっこ」を使用する場合は
Rody Oldenhuis

2

C位、188の 174 187 136バイト

TheLethalCoderの素晴らしいコードゴルフとヒント(オンラインで試してみてください!)に感謝します。

n=>{for(long a,b,c=3;n>0;c++)for(a=2;a<c;a++)for(b=a;b<c;b++)if(a*a*a+b‌​*b*b==c*c*c+1)System‌​.Console.WriteLin‌e(‌​c*c*(a=c)+n/n--);};

最初の10個の数値を見つけるための実行時間:私のi7ラップトップでは33,370842秒(同じタスクの元のバージョンは9,618127秒でした)。

ゴルフされていないバージョン:

using System;

public class Program
{
    public static void Main()
    {
        Action<int> action = n =>
        {
            for (long a, b, d, c = 3; n > 0; c++)
                for (a = 2; a < c; a++)
                    for (b = a; b < c; b++)
                        if (a * a * a + b‌ * b * b == c * c * c + 1)
                            System‌.Console.WriteLin‌e( c * c * (a = c) + n / n--);
        };

        //Called like
        action(5);
    }
}

前のゴルフ187バイトバージョンを含む using System;

using System;static void Main(){for(long a,b,c=3,n=int.Parse(Console.ReadLine());n>0;c++)for(a=2;a<c;a++)for(b=a;b<c;b++)if(a*a*a+b*b*b==c*c*c+1)Console.WriteLin‌​e(c*c*(a=c)+n/n--);}

以前のゴルフ174バイトバージョン(ピーターテイラーのおかげ):

static void Main(){for(long a,b,c=3,n=int.Parse(Console.ReadLine());n>0;c++)for(a=2;a<c;a++)for(b=a;b<c;b++)if(a*a*a+b*b*b==c*c*c+1)Console.WriteLin‌​e(c*c*(a=c)+n/n--);}

前の(オリジナルの)ゴルフ188バイトバージョン(オンラインで試してみてください!):

static void Main(){double a,b,c,d;int t=0,n=Convert.ToInt32(Console.ReadLine());for(c=3;t<n;c++)for(a=2;a<c;a++)for(b=a;b<c;b++){d=(c*c*c)+1;if(a*a*a+b*b*b==d){Console.WriteLine(d);t++;}}}

最初の10個の数字を見つけるための実行時間:私のi7ラップトップでは9,618127秒。

これはC#コーディングでの初めての試みです...他の言語と比べて少し冗長です...


3
1. forループの最初の句で変数を宣言できます。2. int.Parseは、よりも短いConvert.ToInt32。3. longdoubleこのタスクよりも短く、より正確です。4. t不要です:あなたが数えることができるnまで0の代わりに。5.技術的には、3回の偶然の一致がある場合に備えて、印刷後に2つのループを解除する必要があると思います。
ピーターテイラー

2
未テスト:static void Main(){for(long a,b,c=3,n=int.Parse(Console.ReadLine());n>0;c++)for(a=2;a<c;a++)for(b=a;b<c;b++)if(a*a*a+b*b*b==c*c*c+1)Console.WriteLine(c*c*(a=c)+n/n--);}
ピーターテイラー

あなたはまたにコンパイルできるActionメソッドシグネチャIEで使用されるバイトセーブれる()=>{/*code here*/};
TheLethalCoder

あなたはまた、完全名を修飾または追加する必要がありますusing System;バイト・カウントに
TheLethalCoder

@PeterTaylor素晴らしいヒントをありがとう!私は完全にC#の
マリオ

0

GameMaker言語、119バイト

なぜshow_message()そんなに長いのですか:(

x=2y=3z=4w=argument0 while n>0{if x*x*x+y*y*y-z*z*z=1&x<y&y<z{show_message(z*z*z+1)n--}x++if y<x{x=2y++}if z<y{y=3z++}}

x、y、z = 2,3,4 n = input()while n:if y 3 + x 3-z3 == 1and x3 + 1; n- = 1 x + = 1 if y


0

公理、246バイト

h(x:PI):List INT==(r:List INT:=[];i:=0;a:=1;repeat(a:=a+1;b:=1;t:=a^3;repeat(b:=b+1;b>=a=>break;q:=t+b^3;l:=gcd(q-1,223092870);l~=1 and(q-1)rem(l^3)~=0=>0;c:=round((q-1)^(1./3))::INT;if c^3=q-1 then(r:=cons(q,r);i:=i+1;i>=x=>return reverse(r)))))

ungofと結果

-- hh returns x in 1.. numbers in a INT list [y_1,...y_x] such that 
-- for every y_k exist a,b,c in N with y_k=a^3+b^3=c^3+1 
hh(x:PI):List INT==
   r:List INT:=[]
   i:=0;a:=1
   repeat
      a:=a+1
      b:=1
      t:=a^3
      repeat
          b:=b+1
          b>=a=>break
          q:=t+b^3
          l:=gcd(q-1,223092870);l~=1 and (q-1)rem(l^3)~=0 =>0 -- if l|(q-1)=> l^3|(q-1)
          c:=round((q-1.)^(1./3.))::INT
          if c^3=q-1 then(r:=cons(q,r);i:=i+1;output[i,a,b,c];i>=x=>return reverse(r))

(3) -> h 12
   (3)
   [1729, 1092728, 3375001, 15438250, 121287376, 401947273, 3680797185,
    6352182209, 7856862273, 12422690497, 73244501505, 145697644729]
                                                       Type: List Integer             
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.