สามเหลี่ยมพึ่งพา


25

จำนวนสามเหลี่ยมเป็นตัวเลขที่เป็นผลรวมของที่nหมายเลขธรรมชาติตั้งแต่ 1 nถึง ยกตัวอย่าง1 + 2 + 3 + 4 = 10เพื่อให้10เป็นตัวเลขที่เป็นรูปสามเหลี่ยม

กำหนดจำนวนเต็มบวก ( 0 < n <= 10000) เป็นอินพุต (สามารถนำมาเป็นจำนวนเต็มหรือเป็นสตริง) คืนค่าตัวเลขสามเหลี่ยมที่เล็กที่สุดที่เป็นไปได้ที่สามารถเพิ่มลงในอินพุตเพื่อสร้างหมายเลขสามเหลี่ยมอื่น

ตัวอย่างเช่นการป้อนข้อมูลที่26กำหนดการเพิ่ม10ผลลัพธ์36ซึ่งเป็นตัวเลขสามเหลี่ยม ไม่มีตัวเลขสามเหลี่ยมขนาดเล็กกว่า10ที่สามารถเพิ่มลงใน26การสร้างหมายเลขสามเหลี่ยมอื่นดังนั้น10ผลลัพธ์ที่ถูกต้องในกรณีนี้คือ

0 เป็นตัวเลขสามเหลี่ยมดังนั้นหากอินพุตเป็นตัวเลขสามเหลี่ยมตัวเองผลลัพธ์ควรเป็น 0

Testcases

กรณีที่ได้รับในรูปแบบ 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

@ อ็อกซ์ฉันทำผิดพลาดเหมือนกันคุณต้องหาเลขสามเหลี่ยมเพื่อเพิ่มไปยังหมายเลขปัจจุบันเพื่อสร้างหมายเลขสามเหลี่ยมอีกอัน
— Martin Ender

2
ที่เกี่ยวข้อง (ซ้ำกันตามแนวเขต)
— Martin Ender

คำตอบ:


21

Java 8, 58 57 ไบต์

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

ชุดทดสอบออนไลน์

ขอบคุณเดนนิสสำหรับการประหยัดขนาด 1 ไบต์


6
ตอนนี้นี่คือ Java, golfed! :)
— Olivier Grégoire

4
@Computronium คำสั่งของการดำเนินงานมีการค้ำประกันโดย Java Language ข้อกำหนด Java จงใจหลีกเลี่ยงจุดอ่อนของ C.
— Peter Taylor

2
@Computronium docs.oracle.com/javase/tutorial/java/nutsandbolts/ …
— JollyJoker

2
return-~i*i/2;บันทึกเป็นไบต์
— Dennis

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 ไบต์
— Olivier Grégoire

7

MATL , 13 12 ไบต์

ลบ 1 ไบต์โดยใช้แนวคิด (ตั้งจุดตัด) จากคำตอบ 05AB1E ของ Emigna

Q:qYstG-X&X<

ลองออนไลน์!

คำอธิบาย

อนุญาตt(n) = 1 + 2 + ··· + nแสดงว่าเป็นnรูปสามเหลี่ยม -th

รหัสใช้ประโยชน์จากความจริงที่ได้รับ 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

@Giuseppe 8ไม่มีที่ล้มเหลวสำหรับการป้อนข้อมูล เมื่อการส่งออกเท่ากับผูกพันรหัสได้ว่ามันเป็นt(n-1) t(n)-nดังนั้นจึงt(n)จำเป็น ขอบคุณสำหรับความคิดต่อไป!
— Luis Mendo

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 อยู่แล้วเมื่อฉันโพสต์ของฉัน .. ตอนแรกของฉันสั้นลง แต่ดูเหมือนว่าจะไม่อีกต่อไป :)
— Kevin Cruijssen

ขอบคุณ! ใช่คำตอบแรกของฉันซ้ำซ้อนจริงๆ ฉันแก้ไขมันและทำให้มันมีความชาญฉลาดมากขึ้น ฉันจะตรวจสอบของคุณในไม่กี่วินาที!
— Olivier Grégoire

ฉันยังไม่เข้าใจว่าเกิดอะไรขึ้นที่นี่ มันทำงานทำไม คุณกำลังแทนที่ m ทุกครั้งดังนั้นประเด็นคืออะไร
— V. Courtois

2
@ V.Courtois mคำถามที่ถามหาที่เล็กที่สุด ดังนั้นผมจึงไปจากที่ลงไปa 0"แต่คุณอาจจะกำหนด 100 ครั้งค่าเดียวกันa*a+aไปmในb-loop" อ๋อผมไม่ต้องทำมัน 100 ครั้ง แต่ฉันดึงดูดไบต์โดยไม่ทำลายb-loop ก่อนหน้านี้
— Olivier Grégoire

ฉันเห็น @ OlivierGrégoire ดังนั้นการต่อต้านอย่างมีประสิทธิภาพโดยมีวัตถุประสงค์: D
— V. Courtois


4

Neim , 12 9 ไบต์

tS𝕊Λt𝕚)0𝕔

การคำนวณนี้ใช้เวลานานเกินไป (แต่ใช้งานได้ไม่ จำกัด เวลาและหน่วยความจำ) ดังนั้นในลิงก์ที่ฉันสร้างหมายเลขสามเหลี่ยม 143 ตัวแรกเท่านั้นโดยใช้ £𝕖ซึ่งเพียงพอที่จะจัดการอินพุต 10,000 แต่ไม่เพียงพอที่จะหมดเวลา

คำเตือน: สิ่งนี้อาจไม่สามารถใช้งานได้ในรุ่นต่อไป หากเป็นเช่นนั้นให้ทดแทน£ 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 ถึง 10,000 อย่างไร ด้วยอินพุต9998ผลลัพธ์ที่คาดหวังคือ3118753ซึ่งอยู่เหนือหมายเลขสามเหลี่ยม 143 (ซึ่งคือ `10296)
— Olivier Grégoire

@ OlivierGrégoireเพราะThis takes too long to compute (but works given infinite time and memory)
— Stephen

ขอบคุณ @StepHen แต่นั่นไม่ใช่สิ่งที่ฉันพูด สิ่งที่ฉันบอกเป็นนัยก็คือประโยค "ตัวเลขสามเหลี่ยม 143 อันดับแรก [เพียงพอ] ที่จะจัดการอินพุต 10,000" นั้นผิด ฉันยังไม่ได้ทำคณิตศาสตร์ แต่ฉันเชื่อว่าคุณควรต้องการตัวเลขสามเหลี่ยมประมาณ 10,000 (ให้หรือรับ) เพื่อจัดการคดีมากถึง 10,000
— Olivier Grégoire

@ OlivierGrégoireฉันกล่าวว่ามันเพียงพอที่จะรองรับอินพุต 10,000 แต่ไม่น้อยกว่าจำนวนใด ๆ รู้สึกอิสระที่จะเปลี่ยน£เป็นตัวเลขที่สูงขึ้นเช่น 200
— Okx

@ Okk โอเคฉันไม่เข้าใจว่ามันเป็นอย่างนั้นเมื่อฉันอ่านครั้งแรกขอบคุณที่สละเวลาเพื่ออธิบาย :)
— โอลิเวียร์Grégoire

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

Java 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;}

-2 ไบต์ขอบคุณที่@PeterTaylor
-7 ไบต์ขอบคุณที่@JollyJoker
-1 ไบต์ขอบคุณ@ceilingcat @ceilingcat

คำอธิบาย:

ลองออนไลน์

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 บางทีนั่นอาจเป็นสาเหตุที่ยาวนานที่สุด ;) หรือเป็นเพราะคำอธิบายเพิ่มเติมของฉันได้อย่างไร
— Kevin Cruijssen

ไม่ฉันกำลังคิดเกี่ยวกับรหัส ฉันอาจใช้เวลา 15 นาทีหาวิธีแก้ปัญหาของ Peter Taylor ของคุณมีความชัดเจนแม้ไม่มีความคิดเห็น
— 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

Japtap , 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ฉันคิดว่าคุณสามารถบันทึกไบต์ด้วย นอกจากนั้นดูดีมาก :-)
— ETHproductions



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

Python 2 , 78 71 70 ไบต์

เซเว่นไบต์บันทึก Thanx เพื่อOVSและtheespinosa

หนึ่งไบต์มากขึ้นเนื่องจากการบันทึกคำพูดของนีล , x+9เป็น suffisant และการตรวจสอบ0 <= n <= 10000สำหรับตัวเลขจากธรรมชาติทั้งหมด นอกจากนั้นยังได้รับการตรวจสอบสำหรับx+1แทนx+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
จะมีช่วง (x + 9) ไหม
— Neil

2
คุณสามารถใช้{n*(n+1)/2for n in range(999)}แทนอย่างชัดเจนsetและใช้{}แทนsetในบรรทัดที่สาม
— TheEspinosa

2

JavaScript (ES6), 43 42 ไบต์

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

แก้ไข: บันทึก 1 ไบต์ขอบคุณ @PeterTaylor


การตั้งค่าตัวแปรโกลบอลเป็นการละเมิดที่น่าเกลียดของพารามิเตอร์เริ่มต้น +1 แต่ FWIW คุณสามารถบันทึก byte ต่อไปได้ด้วยการแทนที่-++sด้วย--sเพราะฉันทำใน Java เวอร์ชันอิสระของฉัน แต่ค่อนข้างคล้ายกัน (ภาคผนวก: คุณต้องเปลี่ยนการทดสอบเป็น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

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.