三角形の依存関係


25

三角形の数の合計である数でありn、1から自然数n。例えば、1 + 2 + 3 + 4 = 10その10三角形の数です。

0 < n <= 10000入力として正の整数()を指定すると(整数または文字列として取得できます)、入力に追加して別の三角数を作成できる最小の三角数を返します。

たとえば、inputが与えられた26場合、10結果はになり36、これも三角数です。別の三角形番号を作成するために10追加できる三角形番号より小さい三角形番号はないため、この場合は正しい結果になります。2610

0 は三角数です。したがって、入力自体が三角数の場合、出力は 0

テストケース

ケースは次の形式で提供されます input -> output (resulting triangular number)

0     -> 0   (0)
4     -> 6   (10)
5     -> 1   (6)
7     -> 3   (10)
8     -> 28  (36)
10    -> 0   (10)
24    -> 21  (45)
25    -> 3   (28)
26    -> 10  (36)
34    -> 21  (55)
10000 -> 153 (10153)

得点

これはので、各言語で最少のバイトが勝ちます!


じゃない26 -> 2?
— Okx

@Okx同じ間違いを犯しました。現在の番号に追加して別の三角形の番号を作成するには、三角形の番号を見つける必要があります。
— マーティンエンダー

2
関連。(境界線の重複)
— マーティンエンダー

回答:


21

Java 8、58 57バイト

n->{int i=0,m=0;while(n!=0)n+=n<0?++i:--m;return-~i*i/2;}

オンラインテストスイート

1バイトの節約をしてくれたDennisに感謝します。


6
今、これは golfed、Javaのです!:)
— オリビエグレゴワール

4
@Computronium、操作の順序はJava言語仕様で保証されています。Javaは故意にCの弱点のいくつかの回避
— ピーター・テイラー


2
return-~i*i/2;バイトを保存します。
— デニス

1
@Okx Javaは、プリミティブ型の値渡しおよびオブジェクト(配列を含む)の参照渡しです。実際に同じ変数で出力したい場合は、参照渡しのコンテキスト(リンクで明示的に述べられている)にいる必要があります。私が動作する可能性のある参照渡しの唯一の方法int[]は、int引数としてではなく引数を渡すことです。しかし、それは後で配列を扱うことを意味します。これは動作します:x->{int i=0,m=0,n=x[0];while(n!=0)n+=n<0?++i:--m;x[0]=-~i*i/2;}、しかし63バイトです。
— オリビエグレゴワール

7

MATL、13 12バイト

Emignaの05AB1E回答からアイデア(交差点の設定)を使用して1バイト削除

Q:qYstG-X&X<

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

説明

-番目の三角数をt(n) = 1 + 2 + ··· + n示しましょうn。

このコードは、与えられnたソリューションがによって上限されているという事実を利用しt(n-1)ます。これを確認するには、t(n-1) + n等しいことを確認してくださいt(n)。

8例として入力を検討してください。

Q:q   % Input n implicitly. Push [0 1 2 ... n]
      % STACK: [0 1 2 3 4 5 6 7 8]
Ys    % Cumulative sum
      % STACK: [0 1 3 6 10 15 21 28 36]
t     % Duplicate
      % STACK: [0 1 3 6 10 15 21 28 36], [0 1 3 6 10 15 21 28 36]
G-    % Subtract input, element-wise
      % STACK: [0 1 3 6 10 15 21 28 36], [-8 -7 -5 -2  2  7 13 20 28]
X&    % Set intersection
      % STACK: 28
X<    % Minimum of array (in case there are several solutions). Implicit display
      % STACK: 28

Qあなたは有界性についてのあなたの議論によって先頭を削除できますか?
— ジュゼッペ

@Giuseppeいいえ、入力に失敗します8。出力が境界t(n-1)に等しい場合、コードはそれをとして取得しますt(n)-n。だから、t(n)必要です。とにかくアイデアをありがとう!
— ルイスメンドー

7

Java(OpenJDK 8)、83バイト

n->{int m=0,a=n,b;for(;a-->0;)for(b=0;b<=n;)m=2*n+b*~b++==a*~a?a*a+a:m;return m/2;}

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

クレジット


1
いい答えです(いつものように。)。私が私のものを投稿したとき、すでにJavaの答えがあることに気づかなかった。:)
— ケビンクルーッセン

ありがとう!ええ、私の最初の答えは本当に冗長でした。私はそれを修正し、より貪欲になりましたが、より貪欲になりました。すぐに確認します!
— オリビエグレゴワール

ここで何が起こっているのかまだわかりません。なぜ機能しているのですか?毎回mを交換しているので、何がポイントですか?
— V.クルトワ

2
@ V.Courtois質問は最小のものを求めmます。だから私はa下から下に行き0ます。「ただし、-loop で同じ値a*a+aを100倍に割り当てている」とはい、100回行う必要はありませんが、-loopを先に中断しないことでバイトを増やしています。mbb
— オリビエグレゴワール

@OlivierGrégoireが見えます。だから、それは意図的に非効率的です:D
— V.クルトワ


4

ニーム、12 9バイト

tS𝕊Λt𝕚)0𝕔

これは計算に時間がかかりすぎます(ただし、無限の時間とメモリがある場合は動作します)。そのため、リンクでは最初の143個の三角形の数値のみを生成£𝕖します。

警告:これは将来のバージョンでは機能しない可能性があります。その場合、143の代わりに£を使用します

説明:

t                 Infinite list of triangular numbers
 [ 𝕖]             Select the first  v  numbers
 [£ ]                              143
     S𝕊           Subtract the input from each element
       Λ  )       Only keep elements that are
        t𝕚          triangular
           0𝕔     Get the value closest to 0 - prioritising the higher number if tie

それを試してみてください!


最初の143個の三角形の数字は、0〜10000の入力にどのように十分ですか?入力9998では、期待される結果はで3118753、これは143番目の三角形の数( `10296)をはるかに超えています。
— オリビエグレゴワール

@OlivierGrégoireなぜならThis takes too long to compute (but works given infinite time and memory)
— スティーブン

@StepHenに感謝しますが、それは私が言ったことではありません。私が暗示したのは、「最初の143個の三角形の数は10,000の入力を処理するのに十分です」という文が間違っているということです。私は数学を行っていないが、私はあなたの周りに10000(与えるか、またはテイク)三角形番号は10000に例を処理する必要があるべきだと信じている
— オリヴィエ・グレゴワール

@OlivierGrégoire私は10,000の入力を処理するのに十分であると述べましたが、それより少ない数ではありません。£200などのより大きな数値に自由に変更して
— ください。– Okx

@Okxわかりました、最初に読んだときはそのように理解できませんでした。時間をかけて説明してくれてありがとう:)
— オリビエグレゴワール

4

PHP、45バイト

for(;!$$t;$t+=++$i)${$argn+$t}=~+$t;echo~$$t;

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

の短いバリアントです for(;!$r[$t];$t+=++$i)$r[$argn+$t]=~+$t;echo~$r[$t];

拡大

for(;!$$t;  # stop if a triangular number exists where input plus triangular number is a triangular number
$t+=++$i) # make the next triangular number
  ${$argn+$t}=~+$t; # build variable $4,$5,$7,$10,... for input 4 
echo~$$t; # Output result 

PHP、53バ​​イト

for(;$d=$t<=>$n+$argn;)~$d?$n+=++$k:$t+=++$i;echo+$n;

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

新しいものを使用する PHP 7で宇宙船演算子をします

拡大

for(;$d=$t<=>$n+$argn;) # stop if triangular number is equal to input plus triangular number 
  ~$d
    ?$n+=++$k  # raise additional triangular number
    :$t+=++$i; # raise triangular number sum
echo+$n; # Output and cast variable to integer in case of zero

PHP、55バイト

for(;fmod(sqrt(8*($t+$argn)+1),2)!=1;)$t+=++$i;echo+$t;

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


4

ジャワ8、110の 102 100 93 92バイト

n->{int r=0;for(;t(r)<-t(n+r);r++);return r;}int t(int n){for(int j=0;n>0;n-=++j);return n;}

@PeterTaylorのおかげで-2バイト。@JollyJokerの
おかげで-7バイト。@ceilingcatの おかげで-1バイト
。

説明:

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

n->{                  // Method with integer as parameter and return-type
  int r=0;            //  Result-integer (starting at 0)
  for(;t(r)<-t(n+r);  //  Loop as long as neither `r` nor `n+r` is a triangular number
    r++);             //   And increase `r` by 1 after every iteration
  return r;}          //  Return the result of the loop

int t(int n){         // Separate method with integer as parameter and return-type
                      // This method will return 0 if the input is a triangular number
  for(int i=0;n>0;)   //  Loop as long as the input `n` is larger than 0
    n-=++j;           //   Decrease `n` by `j` every iteration, after we've raised `j` by 1
  return n;}          //  Return `n`, which is now either 0 or below 0

1
Javaソリューションの最も読みやすい:)
— JollyJoker

@JollyJokerたぶんそれが一番長い理由です。;)またはそれは私の追加された説明のためですか?
— ケビンCruijssen

いや、コードについて考えていた。おそらく、Peter Taylorのソリューションがどのように機能するかを理解するのに15分かかりました。コメントがなくてもあなたのものは明らかです。
— JollyJoker

3

Brachylog、17 15バイト

⟦{a₀+}ᶠ⊇Ċ-ṅ?∧Ċh

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

説明

⟦                  [0, …, Input]
 {   }ᶠ            Find all…
  a₀+                …Sums of prefixes (i.e. triangular numbers)
       ⊇Ċ          Take an ordered subset of two elements
         -ṅ?       Subtracting those elements results in -(Input)
            ∧Ċh    Output is the first element of that subset

3

Python 2、59バイト

lambda n:min((r-2*n/r)**2/8for r in range(1,2*n,2)if n%r<1)

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

これは、三角数字を取得するtためnに追加できる三角数字の次の特性評価を使用します。

8*t+1 = (r-2*s)^2除数ペア用(r,s)とr*s==nし、r奇数。

コードは、このようなすべての三角形の数の最小値を取ります。


3

ゼリー、8バイト

0r+\ðf_Ḣ

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

使い方

0r+\ðf_Ḣ  Main link. Argument: n

0r        Build [0, ..., n].
  +\      Take the cumulative sum, generating A := [T(0), ..., T(n)].
    ð     Begin a dyadic chain with left argument A and right argument n.
      _   Compute A - n, i.e., subtract n from each number in A.
     f    Filter; keep only numbers of A that appear in A - n.
       Ḣ  Head; take the first result.

3

ジャプト、24 23 16 15バイト

ò å+
m!nNg)æ!øU

試して

おかげで1バイト節約 ETHの


説明

    :Implicit input of integer U.
ò   :Create an array of integers from 0 to U, inclusive.
å+  :Cumulatively reduce by summing. Result is implicitly assigned to variable V.
m   :Map over U.
!n  :From the current element subtract...
Ng  :  The first element in the array of inputs (the original value of U).
æ   :Get the first element that returns true when...
!øU :  Checking if U contains it.
    :Implicit output of resulting integer.

でバイトを節約できると思いますæ!øV。それ以外は素晴らしいです
— ね:



2

Mathematica、62バイト

(s=Min@Abs[m/.Solve[2#==(n-m)(n+m+1),{n,m},Integers]])(s+1)/2&

Mathematicaを知りませんが、Solve[2*#==m(m+1)-n(n+1)短くなります(動作する場合)?
— Kritixi Lithos

はい、ちょうど答えを投稿し、今すぐゴルフしようとしています
— -J42161217

2

パイソン2、78の 71 70バイト

7バイト保存、ovsとtheespinosaに感謝

発言が原因で保存されたもう一つのバイトニールは、x+9suffisantとで確認され、すべての自然数のため0 <= n <= 10000。また、の代わりに検証され、動作します。x+1x+9

x=input()
I={n*-~n/2for n in range(x+1)}
print min(I&{i-x for i in I})

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


2
次のn*-~n/2代わりに使用できますn*(n+1)/2
— -ovs

2
range(x + 9)は機能しますか?
— ニール

2
You can use {n*(n+1)/2for n in range(999)} instead of explicit set and also use {} instead of set in the third line
— TheEspinosa

2

JavaScript (ES6), 43 42 bytes

f=(n,a=s=0)=>n?f(n+=n>0?--s:++a,a):a*++a/2
<input type=number min=0 value=0 oninput=o.textContent=f(+this.value)><pre id=o>0

Edit: Saved 1 byte thanks to @PeterTaylor.


Setting a global variable is a hideous abuse of a default parameter. +1. But FWIW you can save a further byte by replacing -++s with --s, as I did in my independently derived but quite similar Java version. (Addendum: you also need to change the test to n>0).
— Peter Taylor

@PeterTaylor Huh, so the n>s check was a red herring all along!
— Neil

Works not for 8192
— Jörg Hülsermann

@JörgHülsermann If you're referring to the snippet, then your browser's stack size may not be large enough, or you may need a browser with experimental tail call optimisation. Alternatively, if you're using NodeJS for testing, use node --stack_size= to increase its stack size.
— Neil

2

Python 3, 60 44 bytes

f=lambda n,k=1:(8*n+1)**.5%1and f(n+k,k+1)+k

Thanks to @xnor for a suggestion that saved 16 bytes!

Try it online!

Background

Let n be a non-negative integer. If n is the kth triangular number, we have

condition

which means there will be a natural solution if and only if 1 + 8n is an odd, perfect square. Clearly, checking the parity of 1 + 8n is not required.

How it works

The recursive function n accepts a single, non-negative integer as argument. When called with a single argument, k defaults to 1.

First, (8*n+1)**.5%1 tests if n is a triangular number: if (and only if) it is, (8*n+1)**.5 will yield an integer, so the residue from the division by 1 will yield 0.

If the modulus is 0, the and condition will fail, causing f to return 0. If this happens in the initial call to f, note that this is the correct output since n is already triangular.

If the modulus is positive, the and condition holds and f(n+k,k+1)+k gets executed. This calls f again, incrementing n by k and k by 1, then adds k to the result.

When f(n0, k0) finally returns 0, we back out of the recursion. The first argument in the first call was n, the second one n + 1, the third one n + 1 + 2, until finally n0 = n + 1 + … k0-1. Note that n0 - n is a triangular number.

Likewise, all these integers will be added to the innermost return value (0), so the result of the intial call f(n) is n0 - n, as desired.


If you increment n in recursing as well, you can write n rather than (n+k).
— xnor


Wow, that's a lot nicer than what I was trying.
— xnor

2

C# (.NET Core), 291 281 bytes

class p{static int Main(string[]I){string d="0",s=I[0];int c=1,j,k;for(;;){j=k=0;string[]D=d.Split(' '),S=s.Split(' ');for(;j<D.Length;j++)for(;k<S.Length;k++)if(D[j]==S[k])return int.Parse(D[k]);j=int.Parse(D[0])+c++;d=d.Insert(0,$"{j} ");s=s.Insert(0,$"{j+int.Parse(I[0])} ");}}}

Try it online! Program that takes a string as input and outputs through Exit Code.

Saved 10 Bytes thanks to Kevin Cruijssen


1
Hi, welcome to PPCG! You don't need a full program unless the challenge states otherwise. The default is program/function, so a lambda is allowed as well in C#. But if you want to use program, you can golf some things in your current code: class p{static int Main(string[]I){string d="0",s=I[0];int c=1,j,k;for(;;){j=k=0;string[]D=d.Split(' '),S=s.Split(' ');for(;j<D.Length;j++)for(;k<S.Length;k++)if(D[j]==S[k])return int.Parse(D[k]);j=int.Parse(D[0])+c++;d=d.Insert(0,$"{j} ");s=s.Insert(0,$"{j+int.Parse(I[0])} ");}}} (281 bytes)
— Kevin Cruijssen

@KevinCruijssen Thanks for the advice! using for(;;) to make an infinite loop is a nice bump, and I'll make sure to think more carefully about whether using var is actually more efficient than using an explicit type but combining the declarations, and I guess be more diligent in removing unnecessary brackets. As for the program vs. function, I started with a lambda but couldn't get it to run in TIO. I know a TIO link isn't actually necessary, but it's something I like to see in others' answers so I wanted at least something similar in my own.
— Kamil Drakari

I'm also not very good in C# lambdas tbh, I usually codegolf in Java. But I think this should be correct. (252 bytes). Also, in case you haven't seen it yet: Tips for code-golfing in C# and Tips for golfing in <all languages> might be interesting to read through. Again welcome, and +1 from me. Nice first answer. Enjoy your stay. :)
— Kevin Cruijssen

2

JavaScript (ES7), 46 44 bytes

f=(n,x=r=0)=>(8*(n+x)+1)**.5%1?f(n,x+=++r):x

Try it

o.innerText=(
f=(n,x=r=0)=>(8*(n+x)+1)**.5%1?f(n,x+=++r):x
)(i.value=8);oninput=_=>o.innerText=f(+i.value)
<input id=i type=number><pre id=o>


1
Would r=x=0 work?
— Kritixi Lithos

Sadly not, @KritixiLithos.
— Shaggy

1

05AB1E, 8 bytes

ÝηODI-Ãн

Try it online! or as a Test suite

Explanation

Ý          # range [0 ... input]
 η         # prefixes
  O        # sum each
   D       # duplicate
    I-     # subtract input from each
      Ã    # keep only the elements in the first list that also exist in the second list
       н   # get the first (smallest)

1

Dyalog APL, 19 bytes

6 bytes saved thanks to @KritixiLithos

{⊃o/⍨o∊⍨⍵+o←0,+\⍳⍵}

Try it online!

How?

o←0,+\⍳⍵ - assign o the first ⍵ triangular numbers

o/⍨ - filter o by

o∊⍨⍵+o - triangular numbers that summed with ⍵ produce triangulars

⊃ - and take the first


+\⍳⍵ should work instead of what you are using to generate the triangular numbers.
— Kritixi Lithos

I think ⊃ works instead of ⌊/
— Kritixi Lithos



1

Add++, 68 bytes

L,RBFEREsECAAx$pBcB_B]VARBFEREsB]GEi$pGBcB*A8*1+.5^1%!!@A!@*b]EZBF#@

Try it online!, or see the test suite!

Even Java is beating me. I really need to add some set commands to Add++

How it works

L,    - Create a lambda function
      - Example argument:  8
  R   - Range;     STACK = [[1 2 3 4 5 6 7 8]]
  BF  - Flatten;   STACK = [1 2 3 4 5 6 7 8]
  ER  - Range;     STACK = [[1] [1 2] ... [1 2 3 4 5 6 7 8]
  Es  - Sum;       STACK = [1 3 6 10 15 21 28 36]
  EC  - Collect;   STACK = [[1 3 6 10 15 21 28 36]]
  A   - Argument;  STACK = [[1 3 6 10 15 21 28 36] 8]
  A   - Argument;  STACK = [[1 3 6 10 15 21 28 36] 8 8]
  x   - Repeat;    STACK = [[1 3 6 10 15 21 28 36] 8 [8 8 8 8 8 8 8 8]]
  $p  - Remove;    STACK = [[1 3 6 10 15 21 28 36] [8 8 8 8 8 8 8 8]]
  Bc  - Zip;       STACK = [[1 8] [3 8] [6 8] [10 8] [15 8] [21 8] [28 8] [36 8]]
  B_  - Deltas;    STACK = [-7 -5 -2 2 7 13 20 28]
  B]  - Wrap;      STACK = [[-7 -5 -2 2 7 13 20 28]]
  V   - Save;      STACK = []
  A   - Argument;  STACK = [8]
  R   - Range;     STACK = [[1 2 3 4 5 6 7 8]]
  BF  - Flatten;   STACK = [1 2 3 4 5 6 7 8]
  ER  - Range;     STACK = [[1] [1 2] ... [1 2 3 4 5 6 7 8]]
  Es  - Sum;       STACK = [1 3 6 10 15 21 28 36]
  B]  - Wrap;      STACK = [[1 3 6 10 15 21 28 36]]
  G   - Retrieve;  STACK = [[1 3 6 10 15 21 28 36] [-7 -5 -2 2 7 13 20 28]]
  Ei  - Contains;  STACK = [[1 3 6 10 15 21 28 36] [0 0 0 0 0 0 0 1]]
  $p  - Remove;    STACK = [[0 0 0 0 0 0 0 1]]
  G   - Retrieve;  STACK = [[0 0 0 0 0 0 0 1] [-7 -5 -2 2 7 13 20 28]]
  Bc  - Zip;       STACK = [[0 -7] [0 -5] [0 -2] [0 2] [0 7] [0 13] [0 20] [1 28]]
  B*  - Products;  STACK = [0 0 0 0 0 0 0 28]
  A   - Argument;  STACK = [0 0 0 0 0 0 0 28 8]
  8*  - Times 8;   STACK = [0 0 0 0 0 0 0 28 64]
  1+  - Increment; STACK = [0 0 0 0 0 0 0 28 65]
  .5^ - Root;      STACK = [0 0 0 0 0 0 0 28 8.1]
  1%  - Frac part; STACK = [0 0 0 0 0 0 0 28 0.1]
  !!  - To bool;   STACK = [0 0 0 0 0 0 0 28 1]
  @   - Reverse;   STACK = [1 28 0 0 0 0 0 0 0]
  A   - Argument;  STACK = [1 28 0 0 0 0 0 0 0 8] 
  !   - Not;       STACK = [1 28 0 0 0 0 0 0 0 0]
  @   - Reverse;   STACK = [0 0 0 0 0 0 0 0 28 1]
  *   - Multiply;  STACK = [0 0 0 0 0 0 0 0 28]
  b]  - Wrap;      STACK = [0 0 0 0 0 0 0 0 [28]]
  EZ  - Unzero;    STACK = [[28]]
  BF  - Flatten;   STACK = [28]
  #   - Sort;      STACK = [28]
  @   - Reverse;   STACK = [28]

1

R, 46 44 43 41 bytes

function(x,y=cumsum(0:x))y[(x+y)%in%y][1]

Try it online!

An anonymous function with one mandatory argument, x; computes first x+1 triangular numbers as an optional argument to golf out a few curly braces. I used choose before I saw Luis Mendo's Octave answer.

I shaved off a few bytes of Luis Mendo's answer but forgot to use the same idea in my answer.





0

Clojure, 74 bytes

#(nth(for[t[(reductions +(range))]i t :when((set(take 1e5 t))(+ i %))]i)0)
#(nth(for[R[reductions]i(R + %(range)):when((set(R - i(range 1e5)))0)]i)0)

Pick your favourite :) Loops might be shorter...


0

Python 2, 82 bytes

f=lambda n,R=[1]:n-sum(R)and f(n,[R+[R[-1]+1],R[1:]][sum(R)>n])or sum(range(R[0]))

Try it online

This was created by modifying this answer from the related question.


works not for 8192
— Jörg Hülsermann

It doesn't work for that on the related question either, because of the recursion depth. I'm not sure what the consensus is on that.
— mbomb007

Some other answers have the same problem. I give only the info
— Jörg Hülsermann
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.